2.1.2 Collection Classes Explained
Collection classes in Java are part of the Java Collections Framework, which provides a set of interfaces and classes to store and manipulate groups of objects. Understanding these classes is crucial for managing data efficiently in Java SE 11 applications.
Key Concepts
1. List Interface and Implementations
The List
interface is an ordered collection that allows duplicate elements. Common implementations include ArrayList
and LinkedList
.
ArrayList
ArrayList
is a resizable array that provides fast random access. It is suitable for scenarios where frequent retrieval of elements is required.
List<String> arrayList = new ArrayList<>(); arrayList.add("Apple"); arrayList.add("Banana"); System.out.println(arrayList.get(1)); // Output: Banana
LinkedList
LinkedList
is a doubly-linked list implementation that provides fast insertion and deletion. It is suitable for scenarios where frequent modifications are required.
List<String> linkedList = new LinkedList<>(); linkedList.add("Apple"); linkedList.add("Banana"); linkedList.remove(0); System.out.println(linkedList.get(0)); // Output: Banana
2. Set Interface and Implementations
The Set
interface is a collection that does not allow duplicate elements. Common implementations include HashSet
, LinkedHashSet
, and TreeSet
.
HashSet
HashSet
uses a hash table for storage and provides constant-time performance for basic operations. It does not guarantee the order of elements.
Set<String> hashSet = new HashSet<>(); hashSet.add("Apple"); hashSet.add("Banana"); hashSet.add("Apple"); // Duplicate, will not be added System.out.println(hashSet); // Output: [Apple, Banana]
LinkedHashSet
LinkedHashSet
maintains the insertion order of elements and provides constant-time performance for basic operations.
Set<String> linkedHashSet = new LinkedHashSet<>(); linkedHashSet.add("Apple"); linkedHashSet.add("Banana"); System.out.println(linkedHashSet); // Output: [Apple, Banana]
TreeSet
TreeSet
stores elements in a sorted order and provides logarithmic-time performance for basic operations.
Set<String> treeSet = new TreeSet<>(); treeSet.add("Banana"); treeSet.add("Apple"); System.out.println(treeSet); // Output: [Apple, Banana]
3. Map Interface and Implementations
The Map
interface stores key-value pairs and does not allow duplicate keys. Common implementations include HashMap
, LinkedHashMap
, and TreeMap
.
HashMap
HashMap
uses a hash table for storage and provides constant-time performance for basic operations. It does not guarantee the order of elements.
Map<String, Integer> hashMap = new HashMap<>(); hashMap.put("Apple", 1); hashMap.put("Banana", 2); System.out.println(hashMap.get("Apple")); // Output: 1
LinkedHashMap
LinkedHashMap
maintains the insertion order of elements and provides constant-time performance for basic operations.
Map<String, Integer> linkedHashMap = new LinkedHashMap<>(); linkedHashMap.put("Apple", 1); linkedHashMap.put("Banana", 2); System.out.println(linkedHashMap.get("Banana")); // Output: 2
TreeMap
TreeMap
stores elements in a sorted order based on the keys and provides logarithmic-time performance for basic operations.
Map<String, Integer> treeMap = new TreeMap<>(); treeMap.put("Banana", 2); treeMap.put("Apple", 1); System.out.println(treeMap.get("Apple")); // Output: 1
Examples and Analogies
Think of a List
as a shopping list where you can add items in a specific order and can have duplicates. An ArrayList
is like a dynamic shopping list that allows quick access to any item, while a LinkedList
is like a shopping list where each item is connected to the next, making it easy to add or remove items.
A Set
is like a unique collection of items where duplicates are not allowed. A HashSet
is like a bag where items are stored without any specific order, a LinkedHashSet
is like a bag where items are stored in the order they were added, and a TreeSet
is like a bag where items are stored in a sorted order.
A Map
is like a dictionary where each word (key) has a definition (value). A HashMap
is like a dictionary where words are stored without any specific order, a LinkedHashMap
is like a dictionary where words are stored in the order they were added, and a TreeMap
is like a dictionary where words are stored in alphabetical order.
By mastering these collection classes, you can efficiently manage and manipulate data in your Java SE 11 applications, making your code more organized and performant.