Collections in Java

Naveen AnamaneniUpdated · 5 min read

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, Queue and Map.
  • Implementations: ready-to-use classes, such as ArrayList, HashSet, ArrayDeque and HashMap.
  • Algorithms: utility methods that work on collections, such as sorting and searching in the Collections class.

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, size and 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

Interfaces describe behavior; classes implement it. Map stands apart from Collection

The main interfaces are:

InterfaceStoresDuplicates allowed?Main classes
ListAn ordered sequence, accessed by positionYesArrayList, LinkedList
SetUnique elementsNoHashSet, LinkedHashSet, TreeSet
Queue / DequeElements waiting to be processedYesPriorityQueue, ArrayDeque
MapKey-value pairsKeys: no; values: yesHashMap, 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.

FirstCollections.java▶ Try it
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"));
    }
}
Output
List: [Navika, Srihitha, Navika]
Set size: 2
Advitha scored 95

Explanation: 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

  1. Easy: Create a List of five city names with one duplicate, then copy it into a Set and print both.
  2. Harder: Store three students and their marks in a Map, then print each student whose marks are above 90.

Quick check: Collections

4 questions
1Which package holds the Collections Framework?
2Which interface does not allow duplicates?
3Does Map extend Collection?
4Which is a utility class with static methods?

Key Takeaways

Was this tutorial helpful?

Lesson 252 of 482