Collection
Java Collection Framework
A Collection in Java is a framework that provides different interfaces and classes to store and manage groups of objects efficiently. It is the backbone of Java’s data structure, making it easier to perform operations like insertion, deletion, searching, sorting, and manipulation of data.
Advantages
Advantages
Using collections in Java has several benefits:
- Increases Performance: Collections improve speed and efficiency compared to traditional arrays.
- Simplifies Code: It reduces the complexity of handling large amounts of data.
- Reduces Effort: No need to manually implement sorting, searching, or resizing—collections handle it all!
- Interoperability: Collections allow different APIs to work together seamlessly.
- Memory Efficient: Unlike arrays, collections dynamically adjust their size, avoiding memory wastage.
- Built-in Methods: Collections provide pre-defined methods for sorting, searching, and iterating through data.
Types of Collections
Types of Collections
The Java Collection framework have three major types of collections:
1. List (Ordered Collection - Allows Duplicates)
A List is an ordered collection where elements are stored in a sequence, and duplicate values are allowed
Common List Implementations:
Common List Implementations:
- ArrayList – Fast for searching but slow for inserting/deleting elements in the middle.
- LinkedList – Fast for inserting/deleting elements but slower for searching.
- Vector – Similar to ArrayList but thread-safe.
Example:
import java.util.ArrayList;
public class ListExample {
public static void main(String[] args) {
ArrayList names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
System.out.println(names); // Output: [Alice, Bob, Charlie]
}
}
2. Queue (FIFO - First In, First Out Order)
A Queue follows the FIFO (First In, First Out) principle, meaning elements are removed in the same order they were added.
Common Queue Implementations:
A Queue follows the FIFO (First In, First Out) principle, meaning elements are removed in the same order they were added.
Common Queue Implementations:
- PriorityQueue – Elements are ordered based on priority rather than insertion order.
- LinkedList (as a queue) – Works as a FIFO queue.
Example:
import java.util.PriorityQueue;
public class QueueExample {
public static void main(String[] args) {
PriorityQueue pq = new PriorityQueue<>();
pq.add(30);
pq.add(10);
pq.add(20);
System.out.println(pq); // Output: [10, 30, 20] (Sorted automatically)
}
}
3. Set (Unique Collection - No Duplicates Allowed)
A Set is a collection that does not allow duplicate values. It is used when you need to store only unique elements.
Common Set Implementations:
Example:A Set is a collection that does not allow duplicate values. It is used when you need to store only unique elements.
Common Set Implementations:
- HashSet – Unordered set, very fast.
- TreeSet – Sorted set, maintains elements in ascending order.
- LinkedHashSet – Maintains insertion order.
import java.util.HashSet;
public class SetExample {
public static void main(String[] args) {
HashSet numbers = new HashSet<>();
numbers.add(5);
numbers.add(10);
numbers.add(5); // Duplicate, will not be added
System.out.println(numbers); // Output: [5, 10]
}
}
Feature | List (ArrayList) | Queue (PriorityQueue) | Set (HashSet) |
---|---|---|---|
Duplicates Allowed? | Yes | Yes | No |
Ordering Maintained? | Yes | No (Depends on priority) | No (Unordered) |
Index-Based Access? | Yes | No | No |
Common Use Cases | Storing ordered elements with duplicates | Processing tasks (FIFO) | Storing unique elements |
Two Minute Drill
Java Collections provide a powerful way to store and manipulate data efficiently. Depending on your needs:
- Use a List if you need an ordered collection with duplicate values.
- Use a Queue for processing elements in FIFO order.
- Use a Set when you need unique elements without duplicates.