Collection Interface in Java
Last Updated : 27 Oct, 2025
The Collection interface is the root of the Java Collections Framework, defined in the java.util package. It represents a group of individual objects as a single unit and provides basic operations for working with them.
- Dynamic in Nature: Collections can automatically grow or shrink in size, unlike arrays that have a fixed length.
- Stores Homogeneous and Heterogeneous Objects: Can hold same-type or different-type elements based on implementation.
- Easy to Use: Provides convenient methods such as add(), remove(), and clear() to manage elements effortlessly.
- Efficient Traversal: Allows easy access and processing of elements using loops or iterators.
Collection Interface Declaration
public interface Collection<E> extends Iterable<E>
Here, E represents the type of elements stored in the collection.
Object Creation of Collection Interface
Collection<String> fruits = new ArrayList<>();
In Java, we cannot create an object of an interface directly. Instead, we create an object of the ArrayList class that implements the interface and assign it to the interface reference.
Java import java.util.*; public class GFG{ public static void main(String[] args){ // Creating a Collection of String type using // ArrayList implementation Collection<String> fruits = new ArrayList<>(); // Adding elements to the collection fruits.add("Apple"); fruits.add("Banana"); fruits.add("Mango"); // Displaying the collection after removal System.out.println("After Removal: " + fruits); } } OutputAfter Removal: [Apple, Banana, Mango]
Hierarchy of Collection Interface
The Collection interface is part of a hierarchy that extends Iterable, which means collections can be traversed.
Sub-Interfaces of Collection Interface
The subinterfaces of Collection, also called collection types, are:
1. List
public interface List<E> extends Collection<E>
2. Set
- Set represents an unordered collection with no duplicate elements.
- Implementing Classes: HashSet, TreeSet, LinkedHashSet, EnumSet, CopyOnWriteArraySet.
- Declaration:
public interface Set<E> extends Collection<E>
3. SortedSet
- SortedSet extends Set and maintains elements in a sorted order.
- Provides methods to handle range-based operations.
- Implementing Class: TreeSet.
- Declaration:
public interface SortedSet<E> extends Set<E>
4. NavigableSet
- NavigableSet extends SortedSet and provides navigation methods like lower(), floor(), ceiling(), and higher().
- Implementing Class: TreeSet.
- Declaration:
public interface NavigableSet<E> extends SortedSet<E>
5. Queue
public interface Queue<E> extends Collection<E>
5. Deque
- Deque extends Queue and allows elements to be added/removed from both ends.
- Implementing Classes: ArrayDeque, LinkedList.
- Declaration:
public interface Deque<E> extends Queue<E>
Operations on Collection Objects
The Collection interface provides several operations to manipulate data efficiently. Let’s see the most common operations using the ArrayList implementation class.
1. Adding Elements
We can add elements using the add(E e) method for a single element or addAll(Collection c) to add multiple elements.
Java import java.util.*; public class Geeks{ public static void main(String[] args){ // Creating a collection using ArrayList implementation Collection<Integer> numbers = new ArrayList<>(); // Adding individual elements numbers.add(10); numbers.add(20); numbers.add(30); // Adding another collection Collection<Integer> moreNumbers = new ArrayList<>(); moreNumbers.add(40); moreNumbers.add(50); numbers.addAll(moreNumbers); System.out.println("After adding elements: " + numbers); } } OutputAfter adding elements: [10, 20, 30, 40, 50]
2. Removing Elements
Elements can be removed using remove(E e) or removeAll(Collection c) methods.
Java import java.util.*; public class Geeks{ public static void main(String[] args){ Collection<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Mango"); fruits.add("Orange"); System.out.println("Initial Collection: " + fruits); // Remove a specific element fruits.remove("Mango"); System.out.println("After removing Mango: " + fruits); // Remove all elements present in another collection Collection<String> toRemove = new ArrayList<>(); toRemove.add("Apple"); toRemove.add("Banana"); fruits.removeAll(toRemove); System.out.println("After removeAll(): " + fruits); } } OutputInitial Collection: [Apple, Banana, Mango, Orange] After removing Mango: [Apple, Banana, Orange] After removeAll(): [Orange]
3. Accessing Elements
Although the Collection interface doesn’t provide index-based access, its sub-interface List (implemented by ArrayList) allows retrieving elements using the get(int index) method.
Java import java.util.*; public class Geeks{ public static void main(String[] args){ // Using List reference for index-based access List<String> colors = new ArrayList<>(); colors.add("Red"); colors.add("Green"); colors.add("Blue"); System.out.println("Colors List: " + colors); // Accessing elements by index String firstColor = colors.get(0); String lastColor = colors.get(colors.size() - 1); System.out.println("First Color: " + firstColor); System.out.println("Last Color: " + lastColor); } } OutputColors List: [Red, Green, Blue] First Color: Red Last Color: Blue
4. Iterating over a Collection
To iterate over the elements of Collection we can use iterator() method.
Java import java.util.*; public class Geeks { public static void main(String[] args) { Collection<String> l = new LinkedList<>(); l.add("Geeks"); l.add("for"); l.add("Geeks"); System.out.println("The list is: " + l); Iterator<String> it = l.iterator(); System.out.print("Iterator values: "); while (it.hasNext()) { System.out.print(it.next() + " "); } } } OutputThe list is: [Geeks, for, Geeks] Iterator values: Geeks for Geeks
Methods of Collection Interface
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java