DEV Community

araf
araf

Posted on

Java Performance Tuning: List vs Set vs HashMap with Real-World Examples

πŸ”₯ Performance Tuning in Java: List, Set, and HashMap with Real-World Examples

When working with Java collections, choosing the right data structure can make or break your application’s performance β€” especially at scale. In this post, we’ll explore how to optimize performance using List, Set, and HashMap β€” with real-world analogies and benchmarks.


βš™οΈ Quick Overview of Collections

Type Ordered? Allows Duplicates? Fast Lookup? Thread-Safe?
ArrayList βœ… Yes βœ… Yes ❌ No ❌ No
HashSet ❌ No ❌ No βœ… Yes (O(1)) ❌ No
HashMap ❌ No Keys unique βœ… Yes (O(1)) ❌ No

πŸ›’ Real-World Scenario: E-Commerce Inventory System

Imagine you're building a backend system for an online store. You need to:

  1. Keep a list of product names in the order they were added
  2. Avoid adding duplicate SKUs
  3. Find product info quickly by SKU

Let’s see how each collection plays a role:


1️⃣ List – Preserve Insertion Order

List<String> productNames = new ArrayList<>();
productNames.add("iPhone 15");
productNames.add("Galaxy S24");
Enter fullscreen mode Exit fullscreen mode

βœ… Use when order matters
⚠️ Lookup is O(n), so avoid for large lists if you need frequent searches.


2️⃣ Set – Avoid Duplicate SKUs

Set<String> skuSet = new HashSet<>();
skuSet.add("IP15-BLK-128GB"); // returns true
skuSet.add("IP15-BLK-128GB"); // returns false
Enter fullscreen mode Exit fullscreen mode

βœ… Use when you need uniqueness with fast checks
🏎 O(1) lookup for most cases


3️⃣ HashMap – Fast Lookup by Key

Map<String, Product> productMap = new HashMap<>();
productMap.put("IP15-BLK-128GB", new Product("iPhone 15", 999));
Enter fullscreen mode Exit fullscreen mode
Product p = productMap.get("IP15-BLK-128GB"); // O(1) access
Enter fullscreen mode Exit fullscreen mode

βœ… Ideal for key-based retrieval
⚠️ Use proper hashCode() and equals() in your key class if it’s custom


πŸš€ Performance Benchmarks (10 million records)

Operation ArrayList HashSet HashMap
Add elements Fast Fast Fast
Check existence Slow (O(n)) Fast Fast
Lookup by key N/A N/A Very fast

Tested with Java 17 and JMH microbenchmarking on a modern laptop.


πŸ‘‡ Common Pitfalls

  • Using List.contains() for large lists: O(n) β†’ Replace with Set
  • Using HashMap with mutable keys: can break the map!
  • Forgetting to override equals() and hashCode() in custom objects used in Sets or Maps

πŸ’‘ Pro Tip: Combine Collections for Best Results

class ProductRegistry {
    List<Product> productList = new ArrayList<>();
    Set<String> skus = new HashSet<>();
    Map<String, Product> productMap = new HashMap<>();
}
Enter fullscreen mode Exit fullscreen mode

This pattern gives:

  • Ordered data (List)
  • Uniqueness (Set)
  • Fast lookup (Map)

πŸ“¦ TL;DR

Use Case Best Collection
Keep order, allow duplicates ArrayList
Prevent duplicates HashSet
Lookup by key HashMap

πŸ‘¨β€πŸ’» Final Thought

Performance tuning in Java is about more than algorithms β€” it's also about choosing the right data structures for the job. List, Set, and Map each serve a purpose, and mastering when to use which can make your app faster and more efficient.


🧠 Follow for more Java and system design insights!

Top comments (1)

Collapse
 
ganesh_kumar_e4e98249188e profile image
ganesh kumar

Great comparison! Understanding when to use List, Set, or HashMap is key for Java performance. For anyone looking to build strong Java skills, this Java Course in Coimbatore is worth checking out.

Visit - appincoimbatore.com/java-course-in...