Java Collections Framework
The Java Collections Framework (JCF) is a set of classes and interfaces that implement commonly reusable collection data structures. The JCF provides both interfaces and classes to manage groups of objects.
Core Interfaces
- Collection: The root interface of the collections framework.
- List: An ordered collection (also known as a sequence).
- Set: A collection that cannot contain duplicate elements.
- Queue: A collection used to hold multiple elements prior to processing.
- Map: An object that maps keys to values, cannot contain duplicate keys.
Commonly Used Classes
- ArrayList: Resizable-array implementation of the List interface.
- HashSet: Implementation of the Set interface, backed by a hash table.
- HashMap: Hash table-based implementation of the Map interface.
- LinkedList: Doubly-linked list implementation of the List and Deque interfaces.
- PriorityQueue: Implementation of the Queue interface, backed by a priority heap.
Example of Using Collections
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// ArrayList example
ArrayList list = new ArrayList<>();
list.add("Alice");
list.add("Bob");
list.add("Charlie");
System.out.println("ArrayList: " + list);
// HashSet example
HashSet set = new HashSet<>();
set.add("Alice");
set.add("Bob");
set.add("Alice"); // Duplicate element will be ignored
System.out.println("HashSet: " + set);
// HashMap example
HashMap map = new HashMap<>();
map.put("Alice", 1);
map.put("Bob", 2);
System.out.println("HashMap: " + map);
}
}
The Java Collections Framework provides powerful tools for managing and manipulating groups of objects. Stay tuned for more articles and tutorials on Java programming.