集合框架map学习笔记一HashMap
Map:
1、HashMap
2、LinkedHashMap
3、IdentityHashMap
4、WeakHashMap
5、TreeMap
6、EnumMap
7、ConcurrentHashMap
8、ConcurrentSkipListMap
今天主要学习的是HashMap。
1、HashMap
HashMap是根据key的HashCode值存储数据的,如果key的HashCode值冲突的时候,则以链表的形式逐个存放。具有很快的访问速度。但是正是因为这种存放机制,所以HashMap遍历里面的元素的时候,就是无序的。
1.1、HashMap的定义
往往我们使用HashMap的时候配置的参数采用默认配置,桶的容量为16,加载因子为0.75。
加载因子是指当HashMap的元素已经达到容量的75%的时候,就会进行扩充容器。
private abstract class HashIterator<E> implements Iterator<E> { Entry<K,V> next;// next entry to return int expectedModCount;// For fast-fail int index;// current slot Entry<K,V> current;// current entry HashIterator() { expectedModCount = modCount; if (size > 0) { // advance to first entry Entry[] t = table; while (index < t.length && (next = t[index++]) == null) ; } } public final boolean hasNext() { return next != null; } final Entry<K,V> nextEntry() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); Entry<K,V> e = next; if (e == null) throw new NoSuchElementException(); if ((next = e.next) == null) { Entry[] t = table; while (index < t.length && (next = t[index++]) == null) ; } current = e; return e; } public void remove() { if (current == null) throw new IllegalStateException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); Object k = current.key; current = null; HashMap.this.removeEntryForKey(k); expectedModCount = modCount; } }