This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static final int tableSizeFor(int cap) { | |
int n = cap - 1; | |
n |= n >>> 1; | |
n |= n >>> 2; | |
n |= n >>> 4; | |
n |= n >>> 8; | |
n |= n >>> 16; | |
return (n < 0) ? 1 : (n >= 1024) ? 1024 : n + 1; | |
} | |
这段方法,其实cap大于1024的时候,返回1024,这显然可以先放在开头的时候判断并直接返回的,不知道,hashmap为何要在最后。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void writeObject(java.io.ObjectOutputStream s) | |
throws IOException { | |
int buckets = capacity(); | |
// Write out the threshold, loadfactor, and any hidden stuff | |
s.defaultWriteObject(); | |
s.writeInt(buckets); | |
s.writeInt(size); | |
internalWriteEntries(s); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public synchronized V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { | |
Objects.requireNonNull(remappingFunction); | |
Entry<?,?> tab[] = table; | |
int hash = key.hashCode(); | |
int index = (hash & 0x7FFFFFFF) % tab.length; | |
@SuppressWarnings("unchecked") | |
Entry<K,V> e = (Entry<K,V>)tab[index]; | |
for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressWarnings("unchecked") | |
@Override | |
public synchronized void forEach(BiConsumer<? super K, ? super V> action) { | |
Objects.requireNonNull(action); // explicit check required in case | |
// table is empty. | |
final int expectedModCount = modCount; | |
Entry<?, ?>[] tab = table; | |
for (Entry<?, ?> entry : tab) { | |
while (entry != null) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.util.IdentityHashMap类利用哈希表实现 Map 接口,比较键(和值)时使用引用相等性代替对象相等性。 | |
换句话说,在 IdentityHashMap 中,当且仅当 (k1==k2) 时,才认为两个键 k1 和 k2 相等(在正常 Map 实现(如 HashMap)中, | |
当且仅当满足下列条件时才认为两个键 k1 和 k2 相等:(k1==null ? k2==null : e1.equals(e2)))。 | |
此类不是 通用 Map 实现!此类实现 Map 接口时,它有意违反 Map 的常规协定, | |
该协定在比较对象时强制使用 equals 方法。此类设计仅用于其中需要引用相等性语义的罕见情况。 此类的典型用法是拓扑保留对象图形转换,如序列化或深层复制。 | |
要执行这样的转换,程序必须维护用于跟踪所有已处理对象引用的“节点表”。 | |
节点表一定不等于不同对象,即使它们偶然相等也如此。此类的另一种典型用法是维护代理对象。 | |
例如,调试设施可能希望为正在调试程序中的每个对象维护代理对象。 | |
此类提供所有的可选映射操作,并且允许 null 值和 null 键。此类对映射的顺序不提供任何保证;特别是不保证顺序随时间的推移保持不变。 | |
此类提供基本操作(get 和 put)的稳定性能,假定系统标识了将桶间元素正确分开的哈希函数 (System.identityHashCode(Object))。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns a power of two size for the given target capacity. | |
*/ | |
static final int tableSizeFor(int cap) { | |
int n = cap - 1; | |
n |= n >>> 1; | |
n |= n >>> 2; | |
n |= n >>> 4; | |
n |= n >>> 8; | |
n |= n >>> 16; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Initializes or doubles table size. If null, allocates in | |
* accord with initial capacity target held in field threshold. | |
* Otherwise, because we are using power-of-two expansion, the | |
* elements from each bin must either stay at same index, or move | |
* with a power of two offset in the new table. | |
* | |
* @return the table | |
*/ | |
final Node<K,V>[] resize() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static Class<?> comparableClassFor(Object x) { | |
if (x instanceof Comparable) { | |
Class<?> c; Type[] ts, as; Type t; ParameterizedType p; | |
if ((c = x.getClass()) == String.class) // bypass checks | |
return c; | |
if ((ts = c.getGenericInterfaces()) != null) { | |
for (int i = 0; i < ts.length; ++i) { | |
if (((t = ts[i]) instanceof ParameterizedType) && | |
((p = (ParameterizedType)t).getRawType() == | |
Comparable.class) && |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The maximum size of array to allocate. | |
* Some VMs reserve some header words in an array. | |
* Attempts to allocate larger arrays may result in | |
* OutOfMemoryError: Requested array size exceeds VM limit | |
*/ | |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
NewerOlder