Java Collection Framework
The Java Collections Framework is a set of classes and interfaces that implement commonly reusable collection data structures. It provides a unified architecture for representing and manipulating collections, allowing you to manage groups of objects more efficiently.
The Java Collections Framework is essential for working with data structures in Java. By using lists, sets, maps, and queues, you can efficiently manage and manipulate collections of objects.
Interfaces in Java Collections Framework
The core of the Java Collections Framework is its interfaces, which define various types of collections that you can use in Java.
- Collection Interface: The root interface for all collection classes (except maps).
- List Interface: A collection that allows duplicate elements and maintains insertion order.
- Set Interface: A collection that does not allow duplicate elements.
- Queue Interface: A collection designed for holding elements prior to processing (FIFO structure).
- Map Interface: A collection that maps keys to values (like a dictionary).
Implementations of Collection Interfaces
Each interface is implemented by various classes:
- List Implementations:
ArrayList,LinkedList,Vector,Stack.
Example : Using an ArrayList (List) : An ArrayList is backed by a dynamic array. It allows random access to elements and is generally the most efficient implementation for accessing elements by index.
- Use Cases: Ideal for applications where elements are frequently accessed, but insertion and deletion may not be frequent.
- Performance:
- Access: O(1)
- Insert/Delete: O(n) (due to shifting elements after insertion or deletion).
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
// Add elements to the list
list.add("Java");
list.add("Python");
list.add("C++");
// Print all elements in the list
for (String language : list) {
System.out.println(language);
}
}
}2. Set Implementations: HashSet, LinkedHashSet, TreeSet.
Get Avnee Akkewar’s stories in your inbox
Join Medium for free to get updates from this writer.
Example : Using a HashSet (Set) : A HashSet is backed by a hash table and does not guarantee any specific order of elements. It allows constant-time performance for basic operations (add, remove, contains) on average.
- Use Cases: Ideal when you need a collection of unique elements and do not care about the order.
- Performance:
- Add/Remove/Contains: O(1) on average
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
// Add elements to the set
set.add("Java");
set.add("Python");
set.add("Java"); // Duplicate element, will be ignored
// Print all elements in the set
for (String language : set) {
System.out.println(language);
}
}
}3. Queue Implementations: PriorityQueue, LinkedList.
Example : Using a LinkedList : A LinkedList can be used as a queue, where elements are added at the end and removed from the front (FIFO). It provides constant-time performance for these operations.
- Use Cases: Ideal when you need to implement a queue-like structure with efficient insertion and removal at both ends.
- Performance:
- Add/Remove: O(1)
import java.util.LinkedList;
public class PriorityQueueExample {
public static void main(String[] args) {
LinkedList<String> queue = new LinkedList<>();
queue.add("Task 1");
queue.add("Task 2");
System.out.println(queue.removeFirst());
}
}4. Map Implementations: HashMap, TreeMap, LinkedHashMap, Hashtable.
Example : Using a HashMap (Map) : A HashMap is backed by a hash table and stores key-value pairs. It allows constant-time performance for get and put operations, on average.
- Use Cases: Ideal when you need to store and retrieve values based on unique keys.
- Performance:
- Put/Get: O(1) on average
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
// Add key-value pairs
map.put("Java", "Programming Language");
map.put("Python", "Programming Language");
map.put("JavaScript", "Scripting Language");
// Print all key-value pairs
for (String key : map.keySet()) {
System.out.println(key + ": " + map.get(key));
}
}
}





