先上一张Java集合的框架图,便于参考
以下所有特性仅代表自JAVA 1.8
Mapinterface Mapabstract abstractMap extends Map
HashMap: 基本原理
HashMap
1.Hashmap默认初始化容量为16,负载因子默认0.75
2.使用 Entry[] table (数组) 和 Entry
Entry{ final K key; V value; Entry next; final int hash; }
3.HashMap将会自动扩容当内存容量大于 (Capacity * loadFactor)时
4.扩容后的新 Capacity 将会是旧的容量的两倍
5.使用哈希码 hashcode 数据在数组 (Entry[])table中的位置 所以 HashMap是无序的
6.HashMap没有使用同步机制,所以HashMap是线程不安全的
linkedHashMap:
linkedHashMap
1.默认初始化容量为16,负载因子默认0.75
2.使用 Entry[] table (数组)和Entry
static class Entryextends HashMap.Entry { // These fields comprise the doubly linked list used for iteration. Entry before, after; Entry(int hash, K key, V value, HashMap.Entry next) { super(hash, key, value, next); } }
3.linkedHashMap将会自动扩容当内存容量超过(Capacity * loadFactor)
4.新容量Capacity将会是旧的容量的两倍
5.使用哈希码hashcode来计算数据在数组 (Entry[])table中的位置,但是使用双向链表来(doubly linked list) 存储数据和记录顺序,所以linkedHashMap是有序的。
6.linkedHashMap没有使用同步机制,所以linkedHashMap是线程不安全的



