In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand Java 1.7 in the HashMap source code," interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's take you to learn "how to understand Java 1.7 HashMap source code"!
storage structure
The table contains an array of Entry types. Entry stores key pairs. It contains four fields, and from the next field we can see that Entry is a linked list. That is, each position in the array is treated as a bucket, and each bucket stores a linked list. HashMap uses zippers to resolve conflicts. The same linked list stores entries with the same hash value and the same modulo hash bucket capacity.
啊啊
transient Entry[] table; //位桶数组/** * Entry类实现了Map.Entry接口 * 即 实现了getKey()、getValue()、equals(Object o)和hashCode()等方法**/ static class Entry implements Map.Entry { final K key; // 键 V value; // 值 Entry next; // next指针 int hash; //hashCode()方法计算出的hash值 /** * 构造方法,创建一个Entry * 参数:哈希值h,键值k,值v、下一个节点n */ Entry(int h, K k, V v, Entry n) { value = v; next = n; key = k; hash = h; } // 返回 与 此项 对应的键 public final K getKey() { return key; } // 返回 与 此项 对应的值 public final V getValue() { return value; } public final V setValue(V newValue) { V oldValue = value; value = newValue; return oldValue; } /** * equals() * 作用:判断2个Entry是否相等,必须key和value都相等,才返回true */ public final boolean equals(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry e = (Map.Entry)o; Object k1 = getKey(); Object k2 = e.getKey(); if (k1 == k2 || (k1 != null && k1.equals(k2))) { Object v1 = getValue(); Object v2 = e.getValue(); if (v1 == v2 || (v1 != null && v1.equals(v2))) return true; } return false; } /** * hashCode() */ public final int hashCode() { return Objects.hashCode(getKey()) ^ Objects.hashCode(getValue()); } public final String toString() { return getKey() + "=" + getValue(); } /** * 当向HashMap中添加元素时,即调用put(k,v)时, * 对已经在HashMap中k位置进行v的覆盖时,会调用此方法 * 此处没做任何处理 */ void recordAccess(HashMap m) { } /** * 当从HashMap中删除了一个Entry时,会调用该函数 * 此处没做任何处理 */ void recordRemoval(HashMap m) { } }属性成员// 1. 容量(capacity): HashMap中数组的尺子// a. 容量范围:必须是2的幂 &
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 208
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.