Java-Collections Framework学习与总结-HashMap

      开发中常常会用到这样一种数据结构,根据一个关键字,找到所需的信息。这个过程有点像查字典,拿到一个key,去字典表中查找对应的value。Java1.0版本提供了这样的类java.util.Dictionary(抽象类),基本上支持字典表的操作。后来引入了Map接口,更好的描述的这种数据结构。
        一个Map中会包含多个K-V对儿,Key不能重复。一个Key最多只能映射到一个Value,但多个Key可以映射到同一个Value。Map还提供了3中集合视图:Key集合、Value集合和Key-Value集合。
Java代码   收藏代码
  1. public interface Map<K,V> {  
  2.     int size();  
  3.     boolean isEmpty();  
  4.     boolean containsKey(Object key);  
  5.     boolean containsValue(Object value);  
  6.     V get(Object key);  
  7.     V put(K key, V value);  
  8.     V remove(Object key);  
  9.     void putAll(Map<? extends K, ? extends V> m);  
  10.     void clear();  
  11.     Set<K> keySet();  
  12.     Collection<V> values();  
  13.     Set<Map.Entry<K, V>> entrySet();  
  14.     boolean equals(Object o);  
  15.     int hashCode();  
  16.   
  17.     interface Entry<K,V> {  
  18.     K getKey();  
  19.     V getValue();  
  20.     V setValue(V value);  
  21.     boolean equals(Object o);  
  22.     int hashCode();  
  23.     }  
  24.   
  25. }  

        大概看一下Map接口包含的操作。可以看到Map接口中还定义了表示一个K-V的接口Entry。
        我们经常使用的一种实现就是java.util.HashMap。顾名思义,这种实现就是散列表。那什么是散列表呢?首先肯定是一种表,之前总结过表可以由数组或链表实现。假设我们先把数组看成散列表的“表”,而且现在数组中有一些数据。我要用某个关键字在这个数组中直接定位到一个值。可以想象,这个关键字可能是一个编号,一个姓名。。。总之对我们是有意义的标识。但数组怎么能一下定位到某个数据呢?只能靠下标!这就出现了一个问题,我们怎么把有意义的标识转化成下标呢?可以写一个函数来完成这个转化过程,这个函数就可以认为是散列函数了。
        简单起见,先假设key都是int型的整数。假如有一些key:{3,6,7},一个数组int[10]。有了这些前提,先来一个最简单的散列函数:
Java代码   收藏代码
  1. public int hash(int k){  
  2.     return k;  
  3. }  

        (我靠!这也太儿戏了吧。。。行不行啊)对于上面的条件,当然可以了。可以看到,3分配到了下标为3的地方;6分配到下标为6的地方。。。看来是可以的。如果修改一下上面的条件,把key里面3变成3333,肯定不行了,数组太小了。那怎么办?把数组长度改成3333 + 1。这倒是能存了,可是我们为了存3个数,用了这么大一个数组,太多空间浪费掉了。好吧,换一个散列函数:
Java代码   收藏代码
  1. public int hash(int k){  
  2.     return k % table.length;  
  3. }  

        这样的话,又可以继续使用int[10]了。看看还有什么问题,如果再改一下,key变成{3,6,16,7}。又会有问题了,因为6和16会散列到同一个位置。其实,这个问题就是散列表实现过程会常遇到的问题,称为冲突。要消除冲突,一般的方法有分离链表法,开放地址法,再散列法等。(当然,如果上面int数组的长度取一个素数会更好一些。)先分析到这儿吧。
        总之,散列函数的目的是将不同的关键字均匀的散列到表中,散列的越均匀,性能越好。性能可以用对散列表进行插入、删除、查找的时间复杂度太衡量。
        那么java.util.HashMap内部的散列函数是怎样的呢?HashMap内部怎么消除散列冲突问题呢?HashMap内部的表会不会扩展呢?带着这些问题,来看看HashMap的源码。
Java代码   收藏代码
  1. public class HashMap<K,V>  
  2.     extends AbstractMap<K,V>  
  3.     implements Map<K,V>, Cloneable, Serializable  
  4. {  

        可以看到,HashMap是 可克隆的,可序列化的。实现了Map接口。AbstractMap中实现了一些骨架方法,很容易理解,就不看了,继续往下。
Java代码   收藏代码
  1. /** 
  2.      * The default initial capacity - MUST be a power of two. 
  3.      */  
  4.     static final int DEFAULT_INITIAL_CAPACITY = 16;  
  5.   
  6.     /** 
  7.      * The maximum capacity, used if a higher value is implicitly specified 
  8.      * by either of the constructors with arguments. 
  9.      * MUST be a power of two <= 1<<30. 
  10.      */  
  11.     static final int MAXIMUM_CAPACITY = 1 << 30;  
  12.   
  13.     /** 
  14.      * The load factor used when none specified in constructor. 
  15.      */  
  16.     static final float DEFAULT_LOAD_FACTOR = 0.75f;  
  17.   
  18.     /** 
  19.      * The table, resized as necessary. Length MUST Always be a power of two. 
  20.      */  
  21.     transient Entry[] table;  
  22.   
  23.     /** 
  24.      * The number of key-value mappings contained in this map. 
  25.      */  
  26.     transient int size;  
  27.   
  28.     /** 
  29.      * The next size value at which to resize (capacity * load factor). 
  30.      * @serial 
  31.      */  
  32.     int threshold;  
  33.   
  34.     /** 
  35.      * The load factor for the hash table. 
  36.      * 
  37.      * @serial 
  38.      */  
  39.     final float loadFactor;  
  40.   
  41.     /** 
  42.      * The number of times this HashMap has been structurally modified 
  43.      * Structural modifications are those that change the number of mappings in 
  44.      * the HashMap or otherwise modify its internal structure (e.g., 
  45.      * rehash).  This field is used to make iterators on Collection-views of 
  46.      * the HashMap fail-fast.  (See ConcurrentModificationException). 
  47.      */  
  48.     transient volatile int modCount;  

       首先看到,有默认初始化容量和最大容量,默认初始化容量为16,最大容量为2的30次方,而且注释上写了“MUST be a power of two”,不禁让我想起了“a & (b-1)”;默认的加载因子是0.75f(加载因子是干嘛滴?);内部表是数组实现的,类型为java.util.HashMap.Entry。
Java代码   收藏代码
  1. static class Entry<K,V> implements Map.Entry<K,V> {  
  2.     final K key;  
  3.     V value;  
  4.     Entry<K,V> next;  
  5.     final int hash;  

        看来是单向链表实现的;size表示表中实际数据的数量(并非表的长度);threshold和resize有关(看来内部表是会扩展的);一看到modCount就想起了fail-fast吧。
Java代码   收藏代码
  1. /** 
  2.      * Constructs an empty <tt>HashMap</tt> with the specified initial 
  3.      * capacity and load factor. 
  4.      * 
  5.      * @param  initialCapacity the initial capacity 
  6.      * @param  loadFactor      the load factor 
  7.      * @throws IllegalArgumentException if the initial capacity is negative 
  8.      *         or the load factor is nonpositive 
  9.      */  
  10.     public HashMap(int initialCapacity, float loadFactor) {  
  11.         if (initialCapacity < 0)  
  12.             throw new IllegalArgumentException("Illegal initial capacity: " +  
  13.                                                initialCapacity);  
  14.         if (initialCapacity > MAXIMUM_CAPACITY)  
  15.             initialCapacity = MAXIMUM_CAPACITY;  
  16.         if (loadFactor <= 0 || Float.isNaN(loadFactor))  
  17.             throw new IllegalArgumentException("Illegal load factor: " +  
  18.                                                loadFactor);  
  19.   
  20.         // Find a power of 2 >= initialCapacity  
  21.         int capacity = 1;  
  22.         while (capacity < initialCapacity)  
  23.             capacity <<= 1;  
  24.   
  25.         this.loadFactor = loadFactor;  
  26.         threshold = (int)(capacity * loadFactor);  
  27.         table = new Entry[capacity];  
  28.         init();  
  29.     }  
  30.   
  31.     /** 
  32.      * Constructs an empty <tt>HashMap</tt> with the specified initial 
  33.      * capacity and the default load factor (0.75). 
  34.      * 
  35.      * @param  initialCapacity the initial capacity. 
  36.      * @throws IllegalArgumentException if the initial capacity is negative. 
  37.      */  
  38.     public HashMap(int initialCapacity) {  
  39.         this(initialCapacity, DEFAULT_LOAD_FACTOR);  
  40.     }  
  41.   
  42.     /** 
  43.      * Constructs an empty <tt>HashMap</tt> with the default initial capacity 
  44.      * (16) and the default load factor (0.75). 
  45.      */  
  46.     public HashMap() {  
  47.         this.loadFactor = DEFAULT_LOAD_FACTOR;  
  48.         threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);  
  49.         table = new Entry[DEFAULT_INITIAL_CAPACITY];  
  50.         init();  
  51.     }  
  52.   
  53.     /** 
  54.      * Constructs a new <tt>HashMap</tt> with the same mappings as the 
  55.      * specified <tt>Map</tt>.  The <tt>HashMap</tt> is created with 
  56.      * default load factor (0.75) and an initial capacity sufficient to 
  57.      * hold the mappings in the specified <tt>Map</tt>. 
  58.      * 
  59.      * @param   m the map whose mappings are to be placed in this map 
  60.      * @throws  NullPointerException if the specified map is null 
  61.      */  
  62.     public HashMap(Map<? extends K, ? extends V> m) {  
  63.         this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,  
  64.                       DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);  
  65.         putAllForCreate(m);  
  66.     }  

        重点看一下构造方法HashMap(int initialCapacity, float loadFactor),这里的一个关键点就是把内部表的初始容量设置成一个大于等于initialCapacity的2的幂。而且在这个构造方法和无参构造方法的最后都调用了一个init()方法,这个方法是提供给子类的钩子方法。
Java代码   收藏代码
  1. // internal utilities  
  2.   
  3. /** 
  4.  * Initialization hook for subclasses. This method is called 
  5.  * in all constructors and pseudo-constructors (clone, readObject) 
  6.  * after HashMap has been initialized but before any entries have 
  7.  * been inserted.  (In the absence of this method, readObject would 
  8.  * require explicit knowledge of subclasses.) 
  9.  */  
  10. void init() {  
  11. }  
  12.   
  13. /** 
  14.  * Applies a supplemental hash function to a given hashCode, which 
  15.  * defends against poor quality hash functions.  This is critical 
  16.  * because HashMap uses power-of-two length hash tables, that 
  17.  * otherwise encounter collisions for hashCodes that do not differ 
  18.  * in lower bits. Note: Null keys always map to hash 0, thus index 0. 
  19.  */  
  20. static int hash(int h) {  
  21.     // This function ensures that hashCodes that differ only by  
  22.     // constant multiples at each bit position have a bounded  
  23.     // number of collisions (approximately 8 at default load factor).  
  24.     h ^= (h >>> 20) ^ (h >>> 12);  
  25.     return h ^ (h >>> 7) ^ (h >>> 4);  
  26. }  
  27.   
  28. /** 
  29.  * Returns index for hash code h. 
  30.  */  
  31. static int indexFor(int h, int length) {  
  32.     return h & (length-1);  
  33. }  

        先看下indexFor方法,h & (length-1)很熟悉吧,就是h mod length,前提是length是2的幂。hash方法内部很奇怪,又是移位又是异或的,在搞什么灰机?我们可以想一下h mod length这样散列有什么问题,以二进制的角度看,一个数 & (2的n次方-1),就相当于取这个二进制数的低n位。
Java代码   收藏代码
  1. int型 32=25次方  
  2. 789 = 00000000000000000000001100010101  
  3. 789 & (32-1)  
  4.     = 00000000000000000000000000010101  
  5. -614 & (32-1)  
  6. -614 = 11111111111111111111110110011010  
  7.      = 00000000000000000000000000011010  

        假设一些数的低位相同,那么发生散列冲突的概率会很大,为了降低这种冲突的概率,hash方法里做了一些调整,给整数h的各个位上加入了一些随机性。 这里有点资料可做参考。
        散列表会提供添加、删除、查找操作常数平均时间的性能。我们先看下添加的代码。
Java代码   收藏代码
  1. /** 
  2.  * Associates the specified value with the specified key in this map. 
  3.  * If the map previously contained a mapping for the key, the old 
  4.  * value is replaced. 
  5.  * 
  6.  * @param key key with which the specified value is to be associated 
  7.  * @param value value to be associated with the specified key 
  8.  * @return the previous value associated with <tt>key</tt>, or 
  9.  *         <tt>null</tt> if there was no mapping for <tt>key</tt>. 
  10.  *         (A <tt>null</tt> return can also indicate that the map 
  11.  *         previously associated <tt>null</tt> with <tt>key</tt>.) 
  12.  */  
  13. public V put(K key, V value) {  
  14.     if (key == null)  
  15.         return putForNullKey(value);  
  16.     int hash = hash(key.hashCode());  
  17.     int i = indexFor(hash, table.length);  
  18.     for (Entry<K,V> e = table[i]; e != null; e = e.next) {  
  19.         Object k;  
  20.         if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {  
  21.             V oldValue = e.value;  
  22.             e.value = value;  
  23.             e.recordAccess(this);  
  24.             return oldValue;  
  25.         }  
  26.     }  
  27.   
  28.     modCount++;  
  29.     addEntry(hash, key, value, i);  
  30.     return null;  
  31. }  

        逐行分析,首先当key为null时,调用putForNullKey方法。
Java代码   收藏代码
  1. /** 
  2.  * Offloaded version of put for null keys 
  3.  */  
  4. private V putForNullKey(V value) {  
  5.     for (Entry<K,V> e = table[0]; e != null; e = e.next) {  
  6.         if (e.key == null) {  
  7.             V oldValue = e.value;  
  8.             e.value = value;  
  9.             e.recordAccess(this);  
  10.             return oldValue;  
  11.         }  
  12.     }  
  13.     modCount++;  
  14.     addEntry(0null, value, 0);  
  15.     return null;  
  16. }  

        Null Key映射到内部表下标为0的位置上,因为每个位置上是一个单向链表(如果有数据的话),所以这里有一个循环。如果找到了Null Key,将其值覆盖,返回旧值。覆盖过程中还顺便调了一下java.util.HashMap.Entry的recordAccess方法(这个方法也可以看成是钩子方法,当然在HashMap里什么都没做)。如果没找到Null Key,会新添加一个Entry(K-V对)。
Java代码   收藏代码
  1.    /** 
  2.     * Adds a new entry with the specified key, value and hash code to 
  3.     * the specified bucket.  It is the responsibility of this 
  4.     * method to resize the table if appropriate. 
  5.     * 
  6.     * Subclass overrides this to alter the behavior of put method. 
  7.     */  
  8.    void addEntry(int hash, K key, V value, int bucketIndex) {  
  9. Entry<K,V> e = table[bucketIndex];  
  10.        table[bucketIndex] = new Entry<K,V>(hash, key, value, e);  
  11.        if (size++ >= threshold)  
  12.            resize(2 * table.length);  
  13.    }  

        根据给定的参数,新建了一个Entry,放到了对应下标(可以称这样的位置是一个桶)的表头(之所以放在表头是因为最新被添加到HashMap中的数据可能被访问的几率会大一些),然后做判断,看看有没有必要给表扩容。
        继续看put方法,下一行是int hash = hash(key.hashCode())。噢!!原来这里调用了key的hashCode方法。Java的Object类中提供了hashCode方法,所以不管key是什么类型,都会有一个int型的hashCode了。记得之前面试被人问过Object的hashCode的作用是干嘛滴,顿时脑子一片空白。原来是这个作用哦!这样看来,给类提供一个好的hashCode方法是很重要的,假设提供一个巨弱的hashCode方法,比如直接返回1。那么把大量这样的对象作为key,放到HashMap里结果就是,所有的数据都放到了一个桶里,形成了一个单向链表,严重降低了删除和查找操作的性能。继续往下看,int i = indexFor(hash, table.length),用上一步得到的hash值来确定表的下标。后面的操作和putForNullKey里面一样,遍历相应的单链表,找到的话就覆盖,找不到就创建一个Entry。
        有了put方法的分析,get方法也很容易看懂了。
Java代码   收藏代码
  1. /** 
  2.  * Returns the value to which the specified key is mapped, 
  3.  * or {@code null} if this map contains no mapping for the key. 
  4.  * 
  5.  * <p>More formally, if this map contains a mapping from a key 
  6.  * {@code k} to a value {@code v} such that {@code (key==null ? k==null : 
  7.  * key.equals(k))}, then this method returns {@code v}; otherwise 
  8.  * it returns {@code null}.  (There can be at most one such mapping.) 
  9.  * 
  10.  * <p>A return value of {@code null} does not <i>necessarily</i> 
  11.  * indicate that the map contains no mapping for the key; it's also 
  12.  * possible that the map explicitly maps the key to {@code null}. 
  13.  * The {@link #containsKey containsKey} operation may be used to 
  14.  * distinguish these two cases. 
  15.  * 
  16.  * @see #put(Object, Object) 
  17.  */  
  18. public V get(Object key) {  
  19.     if (key == null)  
  20.         return getForNullKey();  
  21.     int hash = hash(key.hashCode());  
  22.     for (Entry<K,V> e = table[indexFor(hash, table.length)];  
  23.          e != null;  
  24.          e = e.next) {  
  25.         Object k;  
  26.         if (e.hash == hash && ((k = e.key) == key || key.equals(k)))  
  27.             return e.value;  
  28.     }  
  29.     return null;  
  30. }  
  31.   
  32. /** 
  33.  * Offloaded version of get() to look up null keys.  Null keys map 
  34.  * to index 0.  This null case is split out into separate methods 
  35.  * for the sake of performance in the two most commonly used 
  36.  * operations (get and put), but incorporated with conditionals in 
  37.  * others. 
  38.  */  
  39. private V getForNullKey() {  
  40.     for (Entry<K,V> e = table[0]; e != null; e = e.next) {  
  41.         if (e.key == null)  
  42.             return e.value;  
  43.     }  
  44.     return null;  
  45. }  

        看下remove方法。
Java代码   收藏代码
  1. /** 
  2.  * Removes the mapping for the specified key from this map if present. 
  3.  * 
  4.  * @param  key key whose mapping is to be removed from the map 
  5.  * @return the previous value associated with <tt>key</tt>, or 
  6.  *         <tt>null</tt> if there was no mapping for <tt>key</tt>. 
  7.  *         (A <tt>null</tt> return can also indicate that the map 
  8.  *         previously associated <tt>null</tt> with <tt>key</tt>.) 
  9.  */  
  10. public V remove(Object key) {  
  11.     Entry<K,V> e = removeEntryForKey(key);  
  12.     return (e == null ? null : e.value);  
  13. }  
  14.   
  15. /** 
  16.  * Removes and returns the entry associated with the specified key 
  17.  * in the HashMap.  Returns null if the HashMap contains no mapping 
  18.  * for this key. 
  19.  */  
  20. final Entry<K,V> removeEntryForKey(Object key) {  
  21.     int hash = (key == null) ? 0 : hash(key.hashCode());  
  22.     int i = indexFor(hash, table.length);  
  23.     Entry<K,V> prev = table[i];  
  24.     Entry<K,V> e = prev;  
  25.   
  26.     while (e != null) {  
  27.         Entry<K,V> next = e.next;  
  28.         Object k;  
  29.         if (e.hash == hash &&  
  30.             ((k = e.key) == key || (key != null && key.equals(k)))) {  
  31.             modCount++;  
  32.             size--;  
  33.             if (prev == e)  
  34.                 table[i] = next;  
  35.             else  
  36.                 prev.next = next;  
  37.             e.recordRemoval(this);  
  38.             return e;  
  39.         }  
  40.         prev = e;  
  41.         e = next;  
  42.     }  
  43.   
  44.     return e;  
  45. }  

        remove方法里,删除时需要先在相应的单向链表里找到要删除的数据,然后通过调整链表的“环”的指针来达到删除效果。删除过程中还顺便调了一下e.recordRemoval方法,这也是个钩子。
        要注意一个费时的操作containsValue。
Java代码   收藏代码
  1.    /** 
  2.     * Returns <tt>true</tt> if this map maps one or more keys to the 
  3.     * specified value. 
  4.     * 
  5.     * @param value value whose presence in this map is to be tested 
  6.     * @return <tt>true</tt> if this map maps one or more keys to the 
  7.     *         specified value 
  8.     */  
  9.    public boolean containsValue(Object value) {  
  10. if (value == null)  
  11.            return containsNullValue();  
  12.   
  13. Entry[] tab = table;  
  14.        for (int i = 0; i < tab.length ; i++)  
  15.            for (Entry e = tab[i] ; e != null ; e = e.next)  
  16.                if (value.equals(e.value))  
  17.                    return true;  
  18. return false;  
  19.    }  
  20.   
  21.    /** 
  22.     * Special-case code for containsValue with null argument 
  23.     */  
  24.    private boolean containsNullValue() {  
  25. Entry[] tab = table;  
  26.        for (int i = 0; i < tab.length ; i++)  
  27.            for (Entry e = tab[i] ; e != null ; e = e.next)  
  28.                if (e.value == null)  
  29.                    return true;  
  30. return false;  
  31.    }  

        这个操作需要对表进行遍历,而且还要循环链表,所以平均执行时间应该是θ(n)。
        再看一下HashMap内部表是怎么扩容的。在添加的时候会将当前表中数据和threshold进行比较,决定是否进行扩容。
Java代码   收藏代码
  1. void addEntry(int hash, K key, V value, int bucketIndex) {  
  2. ry<K,V> e = table[bucketIndex];  
  3.     table[bucketIndex] = new Entry<K,V>(hash, key, value, e);  
  4.     if (size++ >= threshold)  
  5.         resize(2 * table.length);  
  6. }  
  7.   
  8. /** 
  9.  * Rehashes the contents of this map into a new array with a 
  10.  * larger capacity.  This method is called automatically when the 
  11.  * number of keys in this map reaches its threshold. 
  12.  * 
  13.  * If current capacity is MAXIMUM_CAPACITY, this method does not 
  14.  * resize the map, but sets threshold to Integer.MAX_VALUE. 
  15.  * This has the effect of preventing future calls. 
  16.  * 
  17.  * @param newCapacity the new capacity, MUST be a power of two; 
  18.  *        must be greater than current capacity unless current 
  19.  *        capacity is MAXIMUM_CAPACITY (in which case value 
  20.  *        is irrelevant). 
  21.  */  
  22. void resize(int newCapacity) {  
  23.     Entry[] oldTable = table;  
  24.     int oldCapacity = oldTable.length;  
  25.     if (oldCapacity == MAXIMUM_CAPACITY) {  
  26.         threshold = Integer.MAX_VALUE;  
  27.         return;  
  28.     }  
  29.   
  30.     Entry[] newTable = new Entry[newCapacity];  
  31.     transfer(newTable);  
  32.     table = newTable;  
  33.     threshold = (int)(newCapacity * loadFactor);  
  34. }  
  35.   
  36. /** 
  37.  * Transfers all entries from current table to newTable. 
  38.  */  
  39. void transfer(Entry[] newTable) {  
  40.     Entry[] src = table;  
  41.     int newCapacity = newTable.length;  
  42.     for (int j = 0; j < src.length; j++) {  
  43.         Entry<K,V> e = src[j];  
  44.         if (e != null) {  
  45.             src[j] = null;  
  46.             do {  
  47.                 Entry<K,V> next = e.next;  
  48.                 int i = indexFor(e.hash, newCapacity);  
  49.                 e.next = newTable[i];  
  50.                 newTable[i] = e;  
  51.                 e = next;  
  52.             } while (e != null);  
  53.         }  
  54.     }  
  55. }  

        注意数据从旧表到新表时要根据Entry中保存的hash重新定位在新表中的位置。还要注意threshold=capacity * loadFactor,容量和加载因子共同决定了内部表扩容时机。
        注意加载因子对内部表的影响:假设有一个较小的加载因子0.5f,那么当内部表中数据的个数达到表容量一半的时候,便会扩容。这无疑浪费了很多空间,但换来的却是查询和删除的性能。因为散列表的空间大,那么每个桶里的链表的平均长度就会缩短,而一个查找操作做耗费的时间,最坏情况下是计算散列值花费的常数时间加上遍历桶内链表所花费的时间。反过来,一个大的加载因子,会减少一定的空间浪费,但是会增加查找和删除的平均操作时间。所以如果要自己设置加载因子和容量,需要根据实际上下文环境来选择合理的大小。
        有了以上的分析,HashMap内部的迭代器HashIterator也很容易看懂,这里就不分析了。
        最后小总结一下,HashMap是基于散列表实现的一种表示键值对儿集合的集合工具类,对于按照key进行的添加、查找和删除操作,它都提供了平均常数时间的性能。最后注意一点,它是线程不安全的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值