# Verbatim excerpts from the JDK 25.0.5+8 (Temurin) lib/src.zip, for the ankurm.com article # 'HashMap vs ConcurrentHashMap in Java'. Copyright Oracle and/or its affiliates, GPLv2 + Classpath Exception. # Each section lists the source file and the line range it was cut from. ==== A. java.util.HashMap.put [HashMap.java, lines 618-620] ==== public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } ==== A2. java.util.HashMap.hash [HashMap.java, lines 336-339] ==== static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); } ==== B. java.util.HashMap.putVal, the chain-append branch [HashMap.java, lines 647-653] ==== 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; } ==== C. java.util.concurrent.ConcurrentHashMap, the 3-argument constructor [concurrent/ConcurrentHashMap.java, lines 904-914] ==== public ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) { if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0) throw new IllegalArgumentException(); if (initialCapacity < concurrencyLevel) // Use at least as many bins initialCapacity = concurrencyLevel; // as estimated threads long size = (long)(1.0 + (long)initialCapacity / loadFactor); int cap = (size >= (long)MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : tableSizeFor((int)size); this.sizeCtl = cap; } ==== D. ConcurrentHashMap.putVal, first half: how a put picks its path [concurrent/ConcurrentHashMap.java, lines 1022-1034] ==== final V putVal(K key, V value, boolean onlyIfAbsent) { if (key == null || value == null) throw new NullPointerException(); int hash = spread(key.hashCode()); int binCount = 0; for (Node[] tab = table;;) { Node f; int n, i, fh; K fk; V fv; if (tab == null || (n = tab.length) == 0) tab = initTable(); else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) { if (casTabAt(tab, i, null, new Node(hash, key, value))) break; // no lock when adding to empty bin } else if ((fh = f.hash) == MOVED) ==== E. ConcurrentHashMap.treeifyBin, the table-too-small branch [concurrent/ConcurrentHashMap.java, lines 2681-2685] ==== private final void treeifyBin(Node[] tab, int index) { Node b; int n; if (tab != null) { if ((n = tab.length) < MIN_TREEIFY_CAPACITY) tryPresize(n << 1); ==== F. ConcurrentHashMap.sumCount [concurrent/ConcurrentHashMap.java, lines 2586-2595] ==== final long sumCount() { CounterCell[] cs = counterCells; long sum = baseCount; if (cs != null) { for (CounterCell c : cs) if (c != null) sum += c.value; } return sum; } ==== G. ConcurrentHashMap.initTable [concurrent/ConcurrentHashMap.java, lines 2307-2328] ==== private final Node[] initTable() { Node[] tab; int sc; while ((tab = table) == null || tab.length == 0) { if ((sc = sizeCtl) < 0) Thread.yield(); // lost initialization race; just spin else if (U.compareAndSetInt(this, SIZECTL, sc, -1)) { try { if ((tab = table) == null || tab.length == 0) { int n = (sc > 0) ? sc : DEFAULT_CAPACITY; @SuppressWarnings("unchecked") Node[] nt = (Node[])new Node[n]; table = tab = nt; sc = n - (n >>> 2); } } finally { sizeCtl = sc; } break; } } return tab; }