Introduction
In this lesson, we will explore Java's Collections Framework, a vital component for handling groups of objects in Java. We'll delve into the core interfaces such as List, Set, and Map, examine their common implementations, learn how to utilize iterators for traversing collections, apply utility methods for effective data manipulation, and distinguish between mutable and immutable collections.
Core Interfaces: List, Set, and Map
List
A List is an ordered collection that allows duplicate elements. Lists are dynamic arrays in which insertion and deletion can be performed at any position.
- Key Characteristics: Ordered, allows duplicates
Common Implementations
- ArrayList
- LinkedList
- Vector
ArrayList Example
import java.util.*;
public class ListExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Apple"); // Duplicates allowed
System.out.println(list);
}
}
// Output:
// [Apple, Banana, Apple]
Set
Set is a collection that does not allow duplicate elements. Think of it as a collection mechanism that stores unique elements.
- Key Characteristics: Unordered, does not allow duplicates
Common Implementations
- HashSet
- LinkedHashSet
- TreeSet
HashSet Example
import java.util.*;
public class SetExample {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple"); // Duplicates not allowed
System.out.println(set);
}
}
// Output:
// [Apple, Banana]
Map
A Map contains key-value pairs and does not allow duplicate keys.
- Key Characteristics: Maps keys to values, keys are unique
Common Implementations
- HashMap
- LinkedHashMap
- TreeMap
HashMap Example
import java.util.*;
public class MapExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 3);
map.put("Banana", 2);
map.put("Apple", 10); // Key 'Apple' value updated
System.out.println(map);
}
}
// Output:
// {Apple=10, Banana=2}
Traversing Collections with Iterators
Iterators provide a way to access the elements of a collection sequentially without exposing its internal structure.
Iterator Usage Example
import java.util.*;
public class IteratorExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
// Output:
// Apple
// Banana
Collections Utility Methods
Java's Collections class provides static methods for data manipulation.
- Sorting:
Collections.sort(list) - Shuffling:
Collections.shuffle(list) - Reversing:
Collections.reverse(list)
Sorting Example
import java.util.*;
public class CollectionsExample {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(3, 1, 2);
Collections.sort(list);
System.out.println(list);
}
}
// Output:
// [1, 2, 3]
Mutable vs Immutable Collections
- Mutable Collections: These are changeable collections where elements can be added or removed. Most default implementations like
ArrayList,HashSetare mutable. - Immutable Collections: These cannot be changed once created, which ensures thread-safety in multi-threaded environments. They can be created using methods like
Collections.unmodifiableList(),List.of(), etc.
Summary / Key Takeaways
In this lesson on the Introduction to Java Collections Framework, you have learned:
- Understanding of List, Set, and Map with a comparison of their characteristics and use cases.
- Explored List, Set, and Map Implementations with practical coding examples to illustrate their unique properties.
- Utilized Iterators to traverse through collection elements efficiently.
- Leveraged Collections Utility Methods such as sorting and shuffling to manipulate data within collections.
- Recognized Mutable vs Immutable Collections, understanding their differences and applications in Java programs.