从集合框架图来看:Map为collection的子接口,以key-value键值对的形式存储数据
数据插入无序
值可以重复,键不能重复
键可以为null
Iterator<Map.Entry<String,String>> iterator = hashMap.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<String,String> next = iterator.next();
String key = next.getKey();
String value = next.getValue();
System.out.print(key+":"+value+".");
}
Iterator<String> iterator1 = hashMap.keySet().iterator();
while(iterator1.hasNext()){
String next = iterator1.next();
System.out.print(next+".");
}
Iterator<String> iterator2 = hashMap.values().iterator();
while(iterator2.hasNext()){
String next = iterator2.next();
System.out.print(next+" ");
}
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>,
Cloneable, Serializable
底层数据结构
数组+链表
【数组中存储的是一个个entry实体, hash到同一个索引位置的数据通过链表链接起来】
基本属性和默认值
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
哈希表中数组默认初始值大小为16
static final int MAXIMUM_CAPACITY = 1 << 30;
哈希表中数组最大容量值
static final float DEFAULT_LOAD_FACTOR = 0.75f;
默认的加载因子-》用来扩容使用
static final Entry<?,?>[] EMPTY_TABLE = {};
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
static class Entry<K,V>{
final K key; //键值对的key(键)
V value; //键值对的value(值)
Entry<K,V> next; //next节点
int hash; //和key相关的hash
1。指定初始容量、指定加载因子
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;
threshold = initialCapacity;
init();
}
2。通过默认加载因子和默认初始容量
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
3。默认构造容量和默认加载因子
public HashMap() {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}
4。通过map集合来初始化当前集合
public HashMap(Map<? extends K, ? extends V> m) {
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
inflateTable(threshold);
putAllForCreate(m);
}
//扩容完成后哈希函数会发生改变
扩容时创建一个新的table数组,并且将原来集合的元素全部哈希,找到新的对应位置进行插入
void resize(int newCapacity) {
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
Entry[] newTable = new Entry[newCapacity];//创建一个新的table数组
transfer(newTable, initHashSeedAsNeeded(newCapacity));
table = newTable;
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
// transfer函数:(会引起CPU瞬间到100%)
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
for (Entry<K,V> e : table) {
while(null != e) {
Entry<K,V> next = e.next;
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
// 新建节点,元素作为当前索引位置的头部元素进行插入
void createEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
size++;}
常用方法研究:
1. put() 方法研究 *******HashMap的工作原理!!!!!
源码解析:
第一次插入元素,需要对数组进行初始化,数组长度为2的指数, 判断key为null或者非null
当key不为null时,通过key哈希(hashing)来找到数据存在的索引位置,遍历该位置下的链表,(判断key是否存在,存在则替换value,不存在创建新的entry)
当key为null时,存在数组的索引为0的位置,遍历该位置下的链表,查看key为null的节点是否存在,存在的话,将value的值更新为传入的value,若该链表下不存key为null的节点,则创建新的entry节点插入该链表!
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null) //key为null
return putForNullKey(value);
//key不为null
int hash = hash(key);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
//key为null时:
private V putForNullKey(V value) {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}
public V get(Object key) {
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key);
return null == entry ? null : entry.getValue();
}