Posts on Medium Tung Doan

Tung Doan's HashMap and Set Performance Optimization in Android Kotlin

Original post HashMap and Set Performance Optimization in Android Kotlin

Article Summary

Using List.contains() instead of Set.contains() in your Android app? You might be making it 900x slower than it needs to be.

Tung Doan breaks down HashMap and Set performance optimization in Android Kotlin with real benchmarks, memory comparisons, and production-ready code examples. This comprehensive guide shows exactly when to use HashMap vs ArrayMap, HashSet vs List, and how to avoid common pitfalls that kill app performance.

Key Takeaways

Critical Insight

Switching from List to HashSet for lookups provides 100-1000x speedup, while choosing ArrayMap for small datasets (<1000 items) cuts memory overhead by 75%.

Continue reading

What This Article Covers

Problem

Tung Doan found that a User class with a poor hashCode() implementation, one that returns a constant value of 1, causes all HashMap entries to land in the same bucket. This creates collisions that tank performance from O(1) down to O(n).

Solution

Use Objects.hash() to distribute hash values properly across multiple fields. This spreads entries evenly across buckets and takes advantage of Java's built-in hashing algorithm, keeping performance consistent no matter how much data you add.

Impact

With a proper hash function, HashMap lookups stay at O(1) average case regardless of dataset size. You avoid the performance drop that happens when collision chains exceed 8 nodes and Java 8+ converts them to trees.

The article includes a complete production-ready shopping cart ViewModel implementation and a multi-level product filter system that handles thousands of items efficiently.

This page summarizes “HashMap and Set Performance Optimization in Android Kotlin” by Tung Doan, published Jan 3, 2026. All credit for the original work goes to the authors.

Read the original on medium.com (opens in new tab)

AI tools for Android

Browse all AI tools →