美文网首页
Java集合框架(八)— HashMap

Java集合框架(八)— HashMap

作者: Sandy_678f | 来源:发表于2018-12-04 12:22 被阅读0次
    image.png

    HashMap定义

    public class HashMap<K, V> extends AbstractMap<K, V> implements Map<K, V>, Cloneable, Serializable
    

    HashMap 是一个散列表,它存储的内容是键值对(key-value)映射。

    HashMap 继承于AbstractMap,实现了Map、Cloneable、java.io.Serializable接口。
    HashMap 的实现不是同步的,这意味着它不是线程安全的。它的key、value都可以为null。此外,HashMap中的映射不是有序的。

    HashMap 的实例有两个参数影响其性能:“初始容量” 和 “加载因子”。默认加载因子是 0.75,如果初始容量大于最大条目数除以加载因子,则不会发生 rehash 操作。

    HashMap的API

    void                 clear()
    Object               clone()
    boolean              containsKey(Object key)
    boolean              containsValue(Object value)
    Set<Entry<K, V>>     entrySet()
    V                    get(Object key)
    boolean              isEmpty()
    Set<K>               keySet()
    V                    put(K key, V value)
    void                 putAll(Map<? extends K, ? extends V> map)
    V                    remove(Object key)
    int                  size()
    Collection<V>        values()
    

    HashMap的构造函数

    public HashMap() {
            this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
        }
    
    public HashMap(int initialCapacity) {
            this(initialCapacity, DEFAULT_LOAD_FACTOR);
        }
    
    public HashMap(int initialCapacity, float loadFactor) {
            if (initialCapacity < 0)
                throw new IllegalArgumentException("Illegal initial capacity: " +
                                                   initialCapacity);
            if (initialCapacity > MAXIMUM_CAPACITY)
                initialCapacity = MAXIMUM_CAPACITY;
            if (loadFactor <= 0 || Float.isNaN(loadFactor))
                throw new IllegalArgumentException("Illegal load factor: " +
                                                   loadFactor);
            this.loadFactor = loadFactor;
            this.threshold = tableSizeFor(initialCapacity);
        }
    
    public HashMap(Map<? extends K, ? extends V> m) {
            this.loadFactor = DEFAULT_LOAD_FACTOR;
            putMapEntries(m, false);
        }
    

    HashMap遍历

    package HashMapDemo;
    
    import java.util.*;
    
    /**
     * @ClassName: HashMapTest
     * @Author sandy
     * @Date: 2018/12/3
     * @Version v1.0.0
     * @Description: //TODO
     */
    
    public class HashMapTest {
    
        public static void iteratorHashMapByEntrySet(HashMap hashMap){
    
            Iterator iterator = hashMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry entry = (Map.Entry) iterator.next();
                Object key = entry.getKey();
                Object value = entry.getValue();
                System.out.println("key: "+key+" value: "+value);
            }
            System.out.println();
        }
    
        public static void iteratorHashMapByKeySet(HashMap hashMap){
            Iterator iterator = hashMap.keySet().iterator();
            while (iterator.hasNext()){
                Object key =  iterator.next();
                Object value = hashMap.get(key);
                System.out.println("key: "+key+" value: "+value);
            }
            System.out.println();
        }
    
        public static void iteratorHashMapByValueColl(HashMap hashMap){
            Iterator iterator = hashMap.values().iterator();
            while (iterator.hasNext()){
                Object key = iterator.next();
                System.out.println("value: "+key);
            }
            System.out.println();
        }
    
        public static void main(String[] args) {
    
            int val;
            Object key;
            Object value;
            HashMap hashMap = new HashMap();
            Random r = new Random();
    
            for (int i=0; i<5; i++) {
                // 随机获取一个[0,100)之间的数字
                val = r.nextInt(100);
    
                key = String.valueOf(val);
                value = r.nextInt(5);
                // 添加到HashMap中
                hashMap.put(key, value);
            }
    
            iteratorHashMapByEntrySet(hashMap);
            iteratorHashMapByKeySet(hashMap);
            iteratorHashMapByValueColl(hashMap);
    
        }
    }
    

    运行结果:

    key: 88 value: 4
    key: 13 value: 2
    key: 80 value: 3
    key: 42 value: 4
    key: 32 value: 3
    
    key: 88 value: 4
    key: 13 value: 2
    key: 80 value: 3
    key: 42 value: 4
    key: 32 value: 3
    
    value: 4
    value: 2
    value: 3
    value: 4
    value: 3
    

    相关文章

      网友评论

          本文标题:Java集合框架(八)— HashMap

          本文链接:https://www.haomeiwen.com/subject/nvhmcqtx.html