Title: Java HashSet — Mutable hashCode Causes 30% Cache Loss
Open Graph Title: Java HashSet — Mutable hashCode Causes 30% Cache Loss
Description: A cache hit rate dropped from 85% to 55% because mutable 'lastAccessed' orphaned HashSet entries. Fix: treat stored objects as immutable in production.
Open Graph Description: A cache hit rate dropped from 85% to 55% because mutable 'lastAccessed' orphaned HashSet entries. Fix: treat stored objects as immutable in production.
Opengraph URL: https://thecodeforge.io/java/hashset-java/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/java/hashset-java/"
},
"headline": "Java HashSet — Mutable hashCode Causes 30% Cache Loss",
"description": "A cache hit rate dropped from 85% to 55% because mutable 'lastAccessed' orphaned HashSet entries. Fix: treat stored objects as immutable in production.",
"keywords": "Java HashSet — Mutable hashCode Causes 30% Cache Loss, Java, Intermediate, Collections, A cache hit rate dropped from 85% to 55% because mutable 'lastAccessed' orphaned HashSet entries. Fix: treat stored objects as immutable in production.",
"proficiencyLevel": "Intermediate",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-05T00:00:00+00:00",
"dateModified": "2026-09-26T00:00:00+00:00",
"url": "https://thecodeforge.io/java/hashset-java/",
"inLanguage": "en",
"educationalLevel": "Intermediate",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Java", "item": "https://thecodeforge.io/java/"},
{"@type": "ListItem", "position": 3, "name": "Java HashSet — Mutable hashCode Causes 30% Cache Loss", "item": "https://thecodeforge.io/java/hashset-java/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Can a Java HashSet contain null values?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes, but only one null. The first time you add null, it's stored. Any subsequent attempt to add null is silently ignored just like any other duplicate. Be careful: calling hashCode() on null would throw a NullPointerException, so HashSet has special-case logic that places null directly into bucket 0, bypassing the hashCode() call entirely."
}
},
{
"@type": "Question",
"name": "Does HashSet maintain insertion order in Java?",
"acceptedAnswer": {
"@type": "Answer",
"text": "No. HashSet makes no guarantees about iteration order, and the order can change between JVM runs or even after the internal array resizes due to a high load factor. If you need insertion order preserved, use LinkedHashSet instead — it's a drop-in replacement with the same interface but maintains a doubly-linked list across its entries."
}
},
{
"@type": "Question",
"name": "What is the difference between HashSet and HashMap in Java?",
"acceptedAnswer": {
"@type": "Answer",
"text": "A HashMap stores key-value pairs where each key is unique. A HashSet stores only values (no pairs) and guarantees uniqueness among them. Internally, HashSet is literally implemented as a HashMap where your set element is the key and a shared dummy Object constant is the value — so every unique element lookup is just a key lookup in the backing map."
}
},
{
"@type": "Question",
"name": "How does HashSet handle hash collisions?",
"acceptedAnswer": {
"@type": "Answer",
"text": "When two different elements have the same hash code (collision), they are stored in the same bucket. Java 7 and earlier used a linked list per bucket. Starting with Java 8, when a bucket exceeds 8 elements, the list is converted to a balanced tree (O(log n) lookup instead of O(n)). This prevents denial-of-service attacks against deliberately colliding hash codes."
}
},
{
"@type": "Question",
"name": "Is HashSet thread-safe?",
"acceptedAnswer": {
"@type": "Answer",
"text": "No. HashSet is not thread-safe. Concurrent modifications from multiple threads without external synchronization can corrupt the internal data structure, leading to infinite loops, missing elements, or ArrayIndexOutOfBoundsException. Use Collections.synchronizedSet(new HashSet<>()) for simple cases, or ConcurrentHashMap.newKeySet() for better concurrency performance."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large