Collections in Java
A collection in Java is an object that holds a group of other objects, called its elements. The Java Collections Framework is the set of ready-made interfaces and classes in the java.util package that is used to store, search, sort and process such groups. In this lesson, you will learn what the framework is, why it is used, and how its main interfaces and classes are related.
What Is the Collections Framework?
The Collections Framework is a unified architecture for working with groups of objects. It has three parts:
- Interfaces: describe what a collection can do, such as
List,Set,QueueandMap. - Implementations: ready-to-use classes, such as
ArrayList,HashSet,ArrayDequeandHashMap. - Algorithms: utility methods that work on collections, such as sorting and searching in the
Collectionsclass.
Before Java 1.2, Java had only a few unrelated classes like Vector and Hashtable. The framework, added in Java 1.2, gave them one common design.
Why Use the Collections Framework?
An array has a fixed size and very few built-in operations. Collections solve that:
- Dynamic size: a collection grows and shrinks as elements are added and removed.
- Ready-made data structures: lists, sets, queues and maps are already written and tested.
- Common methods: every collection supports
add,remove,contains,sizeand so on, in the same way. - Less code: sorting, searching and shuffling are one method call each.
- Better performance: each class is tuned for a particular kind of use.
Hierarchy of the Collections Framework
The main interfaces are:
| Interface | Stores | Duplicates allowed? | Main classes |
|---|---|---|---|
List | An ordered sequence, accessed by position | Yes | ArrayList, LinkedList |
Set | Unique elements | No | HashSet, LinkedHashSet, TreeSet |
Queue / Deque | Elements waiting to be processed | Yes | PriorityQueue, ArrayDeque |
Map | Key-value pairs | Keys: no; values: yes | HashMap, LinkedHashMap, TreeMap |
Note that Map does not extend Collection, because it stores pairs instead of single elements. It is still part of the framework.
Example: Using a List, a Set and a Map
Let's see an example that uses three different collections.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class FirstCollections {
public static void main(String[] args) {
List<String> attendance = new ArrayList<>();
attendance.add("Navika");
attendance.add("Srihitha");
attendance.add("Navika");
System.out.println("List: " + attendance);
Set<String> uniqueStudents = new HashSet<>(attendance);
System.out.println("Set size: " + uniqueStudents.size());
Map<String, Integer> marks = new HashMap<>();
marks.put("Navika", 91);
marks.put("Advitha", 95);
System.out.println("Advitha scored " + marks.get("Advitha"));
}
}List: [Navika, Srihitha, Navika]
Set size: 2
Advitha scored 95Explanation: The ArrayList keeps every entry in order, including the duplicate "Navika". Copying it into a HashSet removes the duplicate, so the set has only 2 elements. The HashMap stores each student's marks against their name, so a mark can be found directly by the name.
Common Mistakes
Using an array when the number of elements isn't known. Use an ArrayList instead.
Thinking Map is a Collection. It is part of the framework, but it doesn't extend Collection.
Confusing Collection and Collections. Collection is an interface; Collections is a utility class of static methods.
Interview Corner
What is the Collections Framework in Java?
It is a unified architecture in java.util for storing and manipulating groups of objects. It consists of interfaces such as List, Set, Queue and Map, implementation classes such as ArrayList, HashSet, ArrayDeque and HashMap, and algorithms such as sorting and searching in the Collections class. It provides dynamic sizing, reusable data structures and a consistent API.
Why doesn't Map extend the Collection interface?
A Collection holds single elements, while a Map holds key-value pairs. Methods like add(E element) don't make sense for a map, so Map is a separate interface, although it is part of the Collections Framework.
Practice
- Easy: Create a
Listof five city names with one duplicate, then copy it into aSetand print both. - Harder: Store three students and their marks in a
Map, then print each student whose marks are above 90.
Key Takeaways
Was this tutorial helpful?