HashMap
概述
最外层数据存储在Entry数组中,拥有相同hashcode的Entry由next属性关联
/**
* The table, resized as necessary. Length MUST Always be a power of two.
*/
transient Entry[] table;
HashMap内部类
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
final int hash;
/**
* Creates new entry.
*/
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
...省略...
}
图解
用箭头相连的Entry都有相同的hashcode值
ConcurrentHashMap
概述
最外层数据由Segment数组来管理,Segment中数据又由HashEntry来进行管理,最终数据是存在HashEntry对象中,拥有相同hashcode值的对象有HashEntry的next属性来进行关联
/**
* The segments, each of which is a specialized hash table
*/
final Segment<K,V>[] segments;
ConcurrentHashMap内部类
static final class HashEntry<K,V> {
final K key;
final int hash;
volatile V value;
final HashEntry<K,V> next;
HashEntry(K key, int hash, HashEntry<K,V> next, V value) {
this.key = key;
this.hash = hash;
this.next = next;
this.value = value;
}}
图解
用箭头相连的HashEntry都有相同的hashcode值
ArrayList
概述
ArrayList是由数组来存储对象的
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer.
*/
private transient Object[] elementData;
图解
LinkedList
概述
LinkedList数据是存在Entry对象里的,LikedList对象里默认会有一个名为header的Entry对象;
在向LinkedList中添加元素时,会自动修改该对象的next和previous属性,使集合中所有对象通过next和previous关联关系能形成一个闭合的环状结构
LinkedList内部类
private static class Entry<E> {
E element;
Entry<E> next;
Entry<E> previous;
Entry(E element, Entry<E> next, Entry<E> previous) {
this.element = element;
this.next = next;
this.previous = previous;
}
}
图解
以添加第一个元素后结果示例(header元素是创建LinkedList对象后就默认存在的)