Collection Framework in Java
The Java Collection Framework is a set of interfaces and classes used to store, manage, and manipulate groups of objects.
It is mainly available in the java.util package.
Collections are useful when we need to store multiple values dynamically.
Main Collection Interfaces
The major interfaces are:
Collection ├── List ├── Set └── Queue Map
Mapis part of the Collection Framework, but it does not extend theCollectioninterface.
1. List
A List is an ordered collection that allows duplicate elements.
Common implementations:
ArrayListLinkedListVectorStack
Example: ArrayList
import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList<String> students = new ArrayList<>(); students.add("Anuradha"); students.add("Ram"); students.add("Sita"); students.add("Anuradha"); System.out.println(students); } }
Output
[Anuradha, Ram, Sita, Anuradha]
2. Set
A Set is a collection that does not allow duplicate elements.
Common implementations:
HashSetLinkedHashSetTreeSet
Example
import java.util.HashSet; class Main { public static void main(String[] args) { HashSet<String> names = new HashSet<>(); names.add("Anuradha"); names.add("Ram"); names.add("Anuradha"); System.out.println(names); } }
The duplicate "Anuradha" is not stored.
3. Queue
A Queue is generally used to store elements in a processing order. A common queue follows FIFO (First In, First Out).
Common implementations:
PriorityQueueArrayDequeLinkedList
Example
import java.util.LinkedList; import java.util.Queue; class Main { public static void main(String[] args) { Queue<String> queue = new LinkedList<>(); queue.add("A"); queue.add("B"); queue.add("C"); System.out.println(queue); System.out.println(queue.poll()); } }
Output
[A, B, C] A
4. Map
A Map stores data as key-value pairs. Keys are unique, while values can be duplicated.
Common implementations:
HashMapLinkedHashMapTreeMapHashtable
Example
import java.util.HashMap; class Main { public static void main(String[] args) { HashMap<Integer, String> students = new HashMap<>(); students.put(1, "Anuradha"); students.put(2, "Ram"); students.put(3, "Sita"); System.out.println(students); } }
Output
{1=Anuradha, 2=Ram, 3=Sita}
Common Collection Classes
| Class | Description |
|---|---|
ArrayList | Resizable array; maintains insertion order |
LinkedList | List and deque implementation |
HashSet | Stores unique elements without guaranteeing iteration order |
TreeSet | Stores unique elements in sorted order |
HashMap | Stores key-value pairs without guaranteeing iteration order |
TreeMap | Stores key-value pairs sorted by keys |
PriorityQueue | Processes elements according to priority |
Common Collection Methods
| Method | Purpose |
|---|---|
add() | Adds an element |
remove() | Removes an element |
contains() | Checks whether an element exists |
size() | Returns the number of elements |
clear() | Removes all elements |
isEmpty() | Checks whether the collection is empty |
get() | Retrieves an element by index from a List |
put() | Adds a key-value pair to a Map |
Collection vs Array
| Collection | Array |
|---|---|
| Size can generally change dynamically. | Size is fixed after creation. |
| Provides many built-in methods. | Has fewer built-in operations. |
| Stores objects using generic types. | Can store primitives and objects. |
| Part of Java Collection Framework. | Built into the Java language. |
Advantages of Collections
- Dynamic size.
- Easy data manipulation.
- Provides reusable data structures.
- Supports searching and sorting.
- Reduces programming effort.
- Provides different structures for different requirements.
Key Points
- The Collection Framework is mainly available in
java.util. Listallows duplicates and maintains order.Setdoes not allow duplicate elements.Queueis useful for ordered processing.Mapstores data as key-value pairs.- Generics such as
ArrayList<String>provide type safety.
Conclusion
The Java Collection Framework provides ready-made data structures for storing and manipulating groups of objects. List, Set, Queue, and Map are the major collection types, each designed for different data-management requirements.