<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged hash-map</title>
<link>https://ask.clojure.org/index.php/tag/hash-map</link>
<description>Powered by Question2Answer</description>
<item>
<title>Persistent dissoc from a hash collision node can make another key unreachable</title>
<link>https://ask.clojure.org/index.php/15303/persistent-dissoc-from-hash-collision-another-unreachable</link>
<description>&lt;p&gt;Clojure 1.12.7, JDK 21.&lt;/p&gt;
&lt;p&gt;After a persistent &lt;code&gt;dissoc&lt;/code&gt; from a &lt;code&gt;HashCollisionNode&lt;/code&gt;, a key that is still in the map can become unreachable with &lt;code&gt;get&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def a (java.util.ArrayList. [0 42]))
(def b (java.util.ArrayList. [1 11]))
[(hash a) (hash b) (hash [0 42]) (= a [0 42])]
;; =&amp;gt; [1003 1003 1867155179 true]

(def m3 (-&amp;gt; (hash-map a :A b :B)
            (dissoc [0 42])
            (assoc a :A2)))
[m3 (count m3) (get m3 b)]
;; =&amp;gt; [{[1 11] :B, [0 42] :A2} 2 nil]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;m3&lt;/code&gt; prints &lt;code&gt;b&lt;/code&gt; as a key and &lt;code&gt;count&lt;/code&gt; is 2, but &lt;code&gt;(get m3 b)&lt;/code&gt; returns &lt;code&gt;nil&lt;/code&gt;. Hash sets have the same problem:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(-&amp;gt; (hash-set a b) (disj [0 42]) (conj a) (contains? b))
;; =&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The transient path (&lt;code&gt;dissoc!&lt;/code&gt; then &lt;code&gt;assoc!&lt;/code&gt;) returns &lt;code&gt;:B&lt;/code&gt; as expected.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are two &lt;code&gt;ArrayList&lt;/code&gt;s with the same hash, so they share a &lt;code&gt;HashCollisionNode&lt;/code&gt;. The vector &lt;code&gt;[0 42]&lt;/code&gt; is &lt;code&gt;=&lt;/code&gt; to &lt;code&gt;a&lt;/code&gt; but has a different hash (the known mismatch in &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/CLJ-1372&quot;&gt;CLJ-1372&lt;/a&gt;). &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/clojure-1.12.7/src/jvm/clojure/lang/PersistentHashMap.java#L964-L971&quot;&gt;HashCollisionNode.without&lt;/a&gt; builds the new node with the &lt;code&gt;hash&lt;/code&gt; parameter, which is the hash of the key passed to &lt;code&gt;dissoc&lt;/code&gt;, instead of the node&#039;s own &lt;code&gt;this.hash&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;return new HashCollisionNode(null, hash, count - 1, removePair(array, idx/2));
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So the remaining node records 1867155179 instead of 1003. The next &lt;code&gt;assoc&lt;/code&gt; of &lt;code&gt;a&lt;/code&gt; (hash 1003) sees a different hash and splits the two into separate branches, and a lookup of &lt;code&gt;b&lt;/code&gt; follows the branch for 1003, where only &lt;code&gt;a&lt;/code&gt; is.&lt;/p&gt;
&lt;p&gt;Using &lt;code&gt;this.hash&lt;/code&gt; in that constructor call makes &lt;code&gt;(get m3 b)&lt;/code&gt; return &lt;code&gt;:B&lt;/code&gt; and &lt;code&gt;contains?&lt;/code&gt; return true in the examples above. I checked this by compiling the changed class and putting it ahead of the 1.12.7 jar on the classpath.&lt;/p&gt;
&lt;p&gt;This bug is reported at &lt;a rel=&quot;nofollow&quot; href=&quot;https://s.conao3.com/blog/2026/8bda7e&quot;&gt;https://s.conao3.com/blog/2026/8bda7e&lt;/a&gt;&lt;/p&gt;
</description>
<category>Sequences</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/15303/persistent-dissoc-from-hash-collision-another-unreachable</guid>
<pubDate>Tue, 24 Nov 2026 08:32:49 +0000</pubDate>
</item>
</channel>
</rss>