๐ง Java Data Structures & Algorithms: From HashMap to Binary Trees
Welcome to this guide on Java Data Structures and Algorithms! ๐ In this post, we'll explore core data structures, their performance, and common algorithms every Java developer should know. This knowledge is essential for writing efficient, maintainable, and high-performance Java code — and for succeeding in technical interviews.
1. ๐ Java Collections Overview
The Java Collections Framework provides a standard set of interfaces and classes to store, manipulate, and process groups of objects efficiently. It is one of the most important areas in Core Java and is heavily tested in interviews.
Key points:
- Iterable: Root interface that allows traversal using Iterator.
- Collection: Base interface for List, Set, and Queue.
- List: Ordered, allows duplicates (ArrayList, LinkedList).
- Queue: FIFO data structure (LinkedList, PriorityQueue).
- Set: Stores unique elements (HashSet, LinkedHashSet, TreeSet).
- Map: Stores key–value pairs (HashMap, TreeMap, ConcurrentHashMap).
2. ๐️ Core Data Structures & Their Complexity
| Structure | Access | Search | Insert | Delete | Notes |
|---|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) | O(n) | Fixed size |
| Stack | O(n) | O(n) | O(1) | O(1) | LIFO |
| Queue | O(n) | O(n) | O(1) | O(1) | FIFO |
| LinkedList | O(n) | O(n) | O(1) | O(1) | Node-based |
| BST | O(log n) | O(log n) | O(log n) | O(log n) | Balanced tree recommended |
3. ๐ HashMap, Hashtable & ConcurrentHashMap
- HashMap: Not thread-safe, allows one null key and multiple null values.
- Hashtable: Thread-safe, legacy class, no null keys or values.
- ConcurrentHashMap: Thread-safe with high concurrency, preferred in modern Java.
ConcurrentHashMap map = new ConcurrentHashMap<>();
map.put("apple", 5);
map.put("banana", 3);
System.out.println(map.get("apple"));
4. ⚡ Sorting Algorithms Overview
| Algorithm | Average | Notes |
|---|---|---|
| Quick Sort | O(n log n) | Used for primitive arrays |
| Merge Sort | O(n log n) | Stable, used for objects |
| Bubble Sort | O(n²) | Educational only |
5. ๐ List vs Set vs Map
| Feature | List | Set | Map |
|---|---|---|---|
| Duplicates | Allowed | Not allowed | Keys only |
| Ordering | Preserved | Impl dependent | Impl dependent |
| Access | Index-based | No index | Key-based |
6. ๐ง ArrayList vs LinkedList
| ArrayList | LinkedList |
|---|---|
| Dynamic array | Doubly linked list |
| Fast random access | Slow random access |
| Slow middle insert/delete | Fast insert/delete |
| Less memory overhead | More memory usage |
7. ๐ Handling Duplicates in Collections
Removing duplicates is a common interview question.
// Using Set
List list = new ArrayList<>(List.of(1, 2, 2, 3));
list = new ArrayList<>(new HashSet<>(list));
// Using Streams
list = list.stream()
.distinct()
.collect(Collectors.toList());
8. ๐ Collection Conversions
Convert int[] to List<Integer>:
int[] arr = {1, 2, 3};
List list = Arrays.stream(arr)
.boxed()
.collect(Collectors.toList());
Convert List to Set:
List list = List.of("A", "B", "A");
Set set = new HashSet<>(list);
9. ๐ Iterator vs ListIterator vs Enumeration
| Iterator | ListIterator | Enumeration |
|---|---|---|
| All collections | Only List | Legacy |
| Forward only | Forward & backward | Forward only |
| Fail-fast | Fail-fast | Not fail-fast |
| Remove allowed | Add, remove, update | No remove |
10. ๐ฆ Collections.emptyList() vs new ArrayList()
| Collections.emptyList() | new ArrayList() |
|---|---|
| Immutable | Mutable |
| Memory efficient | Creates new object |
| Throws exception on add | Allows add |
11. ๐ Key Takeaways for Developers
- Always choose collections based on semantics first, performance second.
- Prefer modern collections over legacy ones.
- Understand iteration behavior and fail-fast mechanisms.
- Practice collection conversions and duplicate handling — interview favorites.
Labels: Java, Data Structures, Collections, Interview Preparation, Performance, Algorithms
Comments
Post a Comment