Definition:
In Java , a Collection is a framework that provides an architecture to store and manipulate a group of objects. The Java Collections Framework (JCF) includes interfaces, classes, and algorithms for managing groups of objects (such as lists, sets, queues, and maps). It is part of the java.util package and is widely used in handling data in a flexible and efficient way.
Contents
Example:
import java.util.*;
public class CollectionsExample {
public static void main(String[] args) {
// 1. List Example (ArrayList)
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Apple"); // Allows duplicates
System.out.println("Fruits List: " + fruits);
// 2. Set Example (HashSet)
Set<String> uniqueFruits = new HashSet<>(fruits);
System.out.println("Unique Fruits Set: " + uniqueFruits);
// 3. Queue Example (PriorityQueue)
Queue<Integer> numbersQueue = new PriorityQueue<>();
numbersQueue.add(5);
numbersQueue.add(2);
numbersQueue.add(8);
System.out.println("Priority Queue (Head): " + numbersQueue.peek());
System.out.println("Priority Queue (Poll): " + numbersQueue.poll());
System.out.println("Priority Queue (After Poll): " + numbersQueue);
// 4. Map Example (HashMap)
Map<Integer, String> students = new HashMap<>();
students.put(101, "Alice");
students.put(102, "Bob");
students.put(103, "Charlie");
System.out.println("Students Map: " + students);
System.out.println("Student with ID 102: " + students.get(102));
// 5. Sorting List Example
Collections.sort(fruits); // Sort the list alphabetically
System.out.println("Sorted Fruits List: " + fruits);
}
}
Output:Â
Fruits List: [Apple, Banana, Cherry, Apple]
Unique Fruits Set: [Apple, Banana, Cherry]
Priority Queue (Head): 2
Priority Queue (Poll): 2
Priority Queue (After Poll): [5, 8]
Students Map: {101=Alice, 102=Bob, 103=Charlie}
Student with ID 102: Bob
Sorted Fruits List: [Apple, Apple, Banana, Cherry]
Features:
- All collections (like List, Set, Queue, Map) have a common set of methods and are derived from common interfaces like Collection and Map.
- The framework provides algorithms like sorting, searching, shuffling, and reversing elements.
- Java Collections support generics, ensuring type safety at compile time (e.g., List<String>).
- Collections can dynamically grow and shrink in size, providing flexibility compared to arrays.
- Most collections can be traversed using iterators (Iterator, ListIterator), which makes them easier to navigate.
- The framework also provides classes that are synchronized for multi-threaded use, such as Vector or ConcurrentHashMap.
Includes different types of collections like:
- List: Ordered collection that allows duplicates (e.g., ArrayList, LinkedList).
- Set: Unordered collection that does not allow duplicates (e.g., HashSet, TreeSet).
- Map: Collection of key-value pairs (e.g., HashMap, TreeMap).
- Queue: A collection that follows the FIFO (First-In-First-Out) principle (e.g., LinkedList, PriorityQueue).
Advantages:
- The Java Collections Framework provides a high-level abstraction for data structures, making it easy to manage groups of objects with minimal effort.
- Since common data structures and algorithms (like sorting, searching) are already implemented, developers don’t need to write these from scratch.
- Collections are designed to be efficient, with a variety of implementations to suit different use cases. For example, ArrayList is fast for accessing elements, while LinkedList is better for insertions and deletions.
- Collections work well with other parts of the Java platform, such as streams and concurrency utilities.
- Using generics reduces runtime errors by catching type-related mistakes at compile-time.
Uses:
- Collections are used to store data dynamically in a flexible way, unlike arrays, which are of fixed size.
- Collections are used to process groups of objects, like sorting or searching data in lists or maps.
- Useful for managing tasks in queues, such as in job scheduling systems or managing server requests.
- Collections are frequently used to store and manipulate data retrieved from databases.
- Collections (especially maps like HashMap ) are used to implement caches that store frequently accessed data for faster access.