Comprehensive Guide to Java Collection Framework: Interfaces, Implementations, and Usage
This article provides an in‑depth overview of Java's collection framework, detailing the hierarchy of interfaces such as Collection, List, Set, and Map, comparing core implementations like ArrayList, LinkedList, HashSet, TreeSet, and HashMap, and illustrating their usage with practical code examples.
The article introduces the Java Collection Framework, explaining that all collection classes reside in the java.util package and are built upon two root interfaces: Collection and Map. It presents a visual framework diagram and a simplified version to help readers grasp the overall structure.
Overall Analysis
The core of the framework consists of the Collection interface (allowing duplicate elements) and the Map interface (key‑value pairs). Collection branches into List (ordered, allows duplicates) and Set (unordered, no duplicates). The article emphasizes that Map does not extend Collection.
Collection Interface
The Collection interface defines fundamental methods such as add(), addAll(), contains(), toArray(), and provides an iterator() method that returns an Iterator. It has two primary sub‑interfaces: List and Set.
List Interface and Implementations List represents an ordered collection where each element has an index. Common implementations include: ArrayList: a dynamic array, non‑synchronized, fast random access, O(n) amortized addition. LinkedList: a doubly‑linked list, non‑synchronized, slower random access but efficient insertions/removals at ends. Vector: similar to ArrayList but synchronized. Stack: extends Vector to provide LIFO stack operations.
Example code for ArrayList:
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
// ...
}
}Example code for LinkedList (including synchronized wrapper):
List<String> list = Collections.synchronizedList(new LinkedList<>());Set Interface and Implementations Set is an unordered collection that does not allow duplicate elements. Implementations include: HashSet: backed by a HashMap, non‑synchronized, no ordering. LinkedHashSet: maintains insertion order using a linked list. TreeSet: sorted set based on a red‑black tree, supports natural or custom ordering.
Key points about HashSet:
Elements are stored based on their hashCode() and equality via equals().
Only one null element is allowed.
When adding objects, ensure hashCode() and equals() are consistent.
Example demonstrating duplicate handling:
Set<String> set = new HashSet<>();
set.add("Hello");
set.add("world");
set.add("Hello"); // duplicate ignored
System.out.println("Size:" + set.size()); // Size:2Map Interface and Implementations Map stores key‑value pairs and does not extend Collection. Implementations discussed: HashMap: hash‑based, non‑synchronized, allows one null key and multiple null values. LinkedHashMap: preserves insertion order (or access order) using a linked list. TreeMap: sorted map based on a red‑black tree, keys must be comparable or supplied with a comparator.
Sample code iterating over each map type:
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("1", "a");
Iterator<String> it = hashMap.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + "--" + hashMap.get(key));
}Similar blocks are shown for LinkedHashMap and TreeMap, demonstrating their iteration order.
Iterator vs. ListIterator Iterator provides forward traversal with methods hasNext(), next(), and remove(). It can only move in one direction. ListIterator extends Iterator for List types, adding bidirectional traversal ( hasPrevious(), previous()), index access ( nextIndex(), previousIndex()), and modification methods ( set(), add()).
Example of ListIterator usage:
ListIterator<String> it = list.listIterator();
while (it.hasNext()) {
String val = it.next();
if (val.equals("bbb")) {
it.remove();
}
}Differences and Comparisons
The article compares various pairs: ArrayList vs. LinkedList: random access vs. insertion/removal performance. HashTable vs. HashMap: synchronization and null handling. HashMap, Hashtable, LinkedHashMap, TreeMap: ordering, thread safety, and performance trade‑offs. HashSet, LinkedHashSet, TreeSet: ordering and sorting behavior.
Key takeaways include when to choose each implementation based on ordering requirements, concurrency needs, and performance characteristics.
Collections Utility Class
The java.util.Collections class offers static helper methods for sorting, searching, and synchronizing collections. Example shows converting an array to a List, sorting it with Collections.sort(), and printing the sorted values.
Overall, the article serves as a detailed reference for developers to understand, select, and correctly use Java's collection classes and interfaces.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
