Java Collections Framework (JCF) is a set of classes and interfaces used to store, manage, and manipulate groups of objects efficiently. Instead of handling individual variables, collections allow developers to work with multiple elements dynamically.
Why Collections are Needed
Arrays have a fixed size, which makes them less flexible when the amount of data changes. Collections overcome this limitation by providing:
Dynamic resizing
Easy insertion and deletion
Searching and sorting support
Ready-made data structures
For example, storing the names of students can be done more efficiently using collections than arrays.
ArrayList<String> students = new ArrayList<>();
students.add("Priya");
students.add("Sanjay");
students.add("Karthick");
Java Collections Framework Hierarchy
The Collection Framework mainly consists of:
List
Set
Queue
Map (separate from Collection interface)
Iterable
|
Collection
├── List
├── Set
└── Queue
Map (Independent Interface)
List Interface
The List interface stores elements in insertion order and allows duplicate values.
Common List Implementations
1. ArrayList
Dynamic array implementation
Fast retrieval
Allows duplicates
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Priya");
names.add("Kumar");
names.add("Priya");
System.out.println(names);
}
}
Output
[Priya, Kumar, Priya]
2. LinkedList
Uses doubly linked list structure
Efficient insertion and deletion
Allows duplicates
LinkedList<Integer> numbers = new LinkedList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
3. Vector
Similar to ArrayList
Synchronized and thread-safe
Set Interface
Set stores unique elements and does not allow duplicates.
HashSet
No duplicate values
No insertion order guarantee
HashSet<String> cities = new HashSet<>();
cities.add("Chennai");
cities.add("Trichy");
cities.add("Chennai");
System.out.println(cities);
Output
[Chennai, Trichy]
LinkedHashSet
Maintains insertion order
No duplicates
TreeSet
Stores elements in sorted order
No duplicates
Queue Interface
Queue follows the FIFO (First In First Out) principle.
Queue<Integer> queue = new LinkedList<>();
queue.add(10);
queue.add(20);
queue.add(30);
System.out.println(queue.poll());
Output
10
Map Interface
Map stores data in key-value pairs.
HashMap
Stores keys and values
Duplicate keys are not allowed
Values can be duplicated
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Priya");
map.put(2, "Ajith");
System.out.println(map);
Output
{1=Priya, 2=Ajith}
LinkedHashMap
Maintains insertion order
TreeMap
Stores keys in sorted order
Common Methods in Collections
add() Adds an element
remove() Removes an element
contains() Checks whether element exists
size() Returns number of elements
clear() Removes all elements
isEmpty() Checks whether collection is empty
Example:
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
System.out.println(list.size());
Output
2
Advantages of Collections
Dynamic size management.
Built-in searching and sorting methods.
Reduced programming effort.
Improved code reusability.
Better performance through optimized data structures.
Array
Fixed size
Stores same type elements
Limited operations
Less flexible
Collection
Dynamic size
Stores objects dynamically
Rich set of methods
More flexible
Conclusion
Java Collections Framework provides powerful data structures for storing and manipulating data efficiently. Interfaces such as List, Set, Queue, and Map offer different behaviors depending on application requirements. Understanding collections is essential for building scalable and maintainable Java applications.
Top comments (0)