π₯ 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:
- Keep a list of product names in the order they were added
- Avoid adding duplicate SKUs
- 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");
β
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
β
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));
Product p = productMap.get("IP15-BLK-128GB"); // O(1) access
β
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 withSet
-
Using
HashMap
with mutable keys: can break the map! -
Forgetting to override
equals()
andhashCode()
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<>();
}
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)
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...