<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ssiddhesh on Medium]]></title>
        <description><![CDATA[Stories by Ssiddhesh on Medium]]></description>
        <link>https://medium.com/@ssiddhesh64?source=rss-294c06a049bb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1mHEJZ35ncAMmuo-.jpg</url>
            <title>Stories by Ssiddhesh on Medium</title>
            <link>https://medium.com/@ssiddhesh64?source=rss-294c06a049bb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 15:36:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ssiddhesh64/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Why Mutable Objects Break HashSet]]></title>
            <link>https://medium.com/@ssiddhesh64/why-mutable-objects-break-hashset-c8c54069b8fe?source=rss-294c06a049bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8c54069b8fe</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[hashset-internal-working]]></category>
            <category><![CDATA[java-collection]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[java-hashset]]></category>
            <dc:creator><![CDATA[Ssiddhesh]]></dc:creator>
            <pubDate>Sun, 31 May 2026 20:10:00 GMT</pubDate>
            <atom:updated>2026-05-31T21:29:07.237Z</atom:updated>
            <content:encoded><![CDATA[<p>I recently came across a deceptively simple Java example where an object was added to a HashSet, modified afterward, and suddenly could no longer be found using contains(). At first glance, it looked like a bug in Java itself.</p><p>Digging deeper led me into the internals of HashSet and HashMap, bucket selection, hashCode contracts, and one of the most common mistakes developers make with mutable objects.</p><p>In this article, I’ll explain why this happens, how HashSet actually works under the hood, and the best practices to avoid subtle bugs in production systems.</p><blockquote>Most developers expect this to print true.</blockquote><pre>import java.util.HashSet;<br>import java.util.Set;<br><br>class Employee {<br><br>    int id;<br>    String name;<br><br>    //Constructor<br><br>    @Override<br>    public boolean equals(Object o) {<br>        if (!(o instanceof Employee))<br>            return false;<br><br>        Employee e = (Employee) o;<br><br>        return id == e.id;<br>    }<br><br>    @Override<br>    public int hashCode() {<br>        return Integer.hashCode(id);<br>    }<br>}<br><br>public class Main {<br><br>    public static void main(String[] args) {<br><br>        Set&lt;Employee&gt; set = new HashSet&lt;&gt;();<br>        Employee emp = new Employee(1, &quot;Sid&quot;);<br>        set.add(emp);<br><br>        emp.id = 2;<br>        System.out.println(set.contains(emp));<br>    }<br>}</pre><p>But in reality the output is <strong><em>false</em></strong></p><p>Now, let’s dive deep why this happens with internal of HashSet</p><pre>// HashSet -&gt; internally uses -&gt; HashMap<br>// In fact:<br>set.add(emp);<br><br>// becomes roughly:<br>map.put(emp, PRESENT);<br><br>// where<br>private static final Object PRESENT = new Object();</pre><p>HashSet stores objects as keys inside a HashMap.</p><h3>What happens during add()</h3><p>When we execute:</p><pre>Employee emp = new Employee(1, “Sid”);<br>set.add(emp);</pre><p>Java calculates:</p><pre>emp.hashCode()  // 1</pre><p>Suppose HashMap currently has 16 buckets<br>Bucket calculation: bucket = hash &amp; (n — 1)<br>For simplicity: let’s say 1 % 16 = 1<br>Object goes into bucket 1.<br>Visualize:</p><pre>Bucket 0<br>Bucket 1 -&gt; Employee(id=1)<br>Bucket 2<br>Bucket 3<br>...</pre><p>Everything is fine.</p><h4>Then disaster happens<br>We mutate:</h4><pre>emp.id = 2;</pre><p>hashcode() depends on id<br>So now</p><pre>emp.hashCode()</pre><p>returns <br>2</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8c54069b8fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>