您好,登录后才能下订单哦!
在Java编程中,HashMap
和HashSet
是两个非常常用的数据结构。它们都基于哈希表实现,具有高效的查找、插入和删除操作。本文将深入分析HashMap
和HashSet
的源码,探讨它们的实现原理、核心数据结构以及常见操作的具体实现。
HashMap
是Java集合框架中的一个重要类,它实现了Map
接口,提供了键值对的存储和检索功能。HashMap
允许null
键和null
值,并且是非线程安全的。
HashMap
的核心数据结构是一个数组,数组中的每个元素是一个链表或红黑树的头节点。这个数组被称为table
,它的每个元素被称为bucket
。
transient Node<K,V>[] table;
Node
是HashMap
中的一个静态内部类,表示键值对的节点:
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
public final int hashCode() { return Objects.hashCode(key) ^ Objects.hashCode(value); }
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
public final boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
}
HashMap
提供了多个构造方法,最常用的是无参构造方法和指定初始容量的构造方法。
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
DEFAULT_LOAD_FACTOR
是默认的负载因子,值为0.75。负载因子决定了HashMap
在扩容时的阈值。
put
方法是HashMap
中最核心的方法之一,用于将键值对插入到HashMap
中。
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
putVal
方法的执行流程如下:
table
是否为空,如果为空则进行初始化。bucket
。bucket
为空,则直接插入新节点。bucket
不为空,则遍历链表或红黑树,查找是否已经存在相同的键。size
超过阈值,则进行扩容。get
方法用于根据键获取对应的值。
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
getNode
方法的执行流程如下:
bucket
。bucket
为空,则返回null
。bucket
的第一个节点就是目标节点,则直接返回。bucket
的第一个节点不是目标节点,则遍历链表或红黑树,查找目标节点。null
。resize
方法用于扩容HashMap
。
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null)
loTail.next = null;
newTab[j] = loHead;
newTab[j + oldCap] = hiHead;
}
}
}
}
return newTab;
}
resize
方法的执行流程如下:
table
。table
中的节点重新分配到新table
中。table
。remove
方法用于根据键删除对应的键值对。
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
else if ((e = p.next) != null) {
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
else {
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}
removeNode
方法的执行流程如下:
bucket
。bucket
为空,则返回null
。bucket
的第一个节点就是目标节点,则直接删除。bucket
的第一个节点不是目标节点,则遍历链表或红黑树,查找目标节点。null
。HashMap
是非线程安全的,在多线程环境下可能会出现数据不一致的问题。为了解决这个问题,可以使用Collections.synchronizedMap
方法将HashMap
包装成线程安全的Map
,或者使用ConcurrentHashMap
。
HashSet
是Java集合框架中的一个重要类,它实现了Set
接口,提供了不重复元素的存储和检索功能。HashSet
允许null
元素,并且是非线程安全的。
HashSet
的核心数据结构是一个HashMap
,它使用HashMap
来存储元素。
private transient HashMap<E,Object> map;
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
HashSet
中的元素实际上是HashMap
中的键,而值则是一个固定的PRESENT
对象。
HashSet
提供了多个构造方法,最常用的是无参构造方法和指定初始容量的构造方法。
public HashSet() {
map = new HashMap<>();
}
public HashSet(int initialCapacity) {
map = new HashMap<>(initialCapacity);
}
add
方法用于向HashSet
中添加元素。
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
add
方法的执行流程如下:
HashMap
的put
方法,将元素作为键,PRESENT
作为值插入到HashMap
中。put
方法返回null
,则表示插入成功,返回true
;否则返回false
。remove
方法用于从HashSet
中删除元素。
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
remove
方法的执行流程如下:
HashMap
的remove
方法,删除指定的键。remove
方法返回PRESENT
,则表示删除成功,返回true
;否则返回false
。contains
方法用于判断HashSet
中是否包含指定的元素。
public boolean contains(Object o) {
return map.containsKey(o);
}
contains
方法的执行流程如下:
HashMap
的containsKey
方法,判断指定的键是否存在。true
;否则返回false
。HashSet
是基于HashMap
实现的,它使用HashMap
来存储元素,并且只使用了HashMap
的键部分。因此,HashSet
的很多操作实际上都是通过调用HashMap
的相应方法来实现的。
HashMap
和HashSet
是Java中非常常用的数据结构,它们都基于哈希表实现,具有高效的查找、插入和删除操作。通过深入分析它们的源码,我们可以更好地理解它们的实现原理和使用场景。在实际开发中,应根据具体需求选择合适的数据结构,并注意它们的线程安全问题。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。