The Wayback Machine - https://web.archive.org/web/20261126125018/https://www.geeksforgeeks.org/java/collection-interface-in-java-with-examples/

Collection Interface in Java

Last Updated : 24 Nov, 2026

The Collection interface in Java is the root interface of the collection hierarchy in the Java Collections Framework. It represents a group of objects and provides common methods to manage and process elements.

  • It supports operations such as adding, removing, searching, and iterating over elements.
  • Interfaces such as List, Set, and Queue extend Collection directly or indirectly.
Java
import java.util.ArrayList;
import java.util.Collection;
public class Geeks{
    public static void main(String[] args){

        // Create a collection using ArrayList
        Collection<String> fruits = new ArrayList<>();

        // Add elements
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Mango");

        // Remove an element
        fruits.remove("Banana");
        System.out.println("After Removal: " + fruits);
        System.out.println("Contains Apple: " + fruits.contains("Apple"));
        System.out.println("Size: " + fruits.size());

        // Iterate over elements
        for (String fruit : fruits){
            System.out.println(fruit);
        }
    }
}

Output
After Removal: [Apple, Mango]
Contains Apple: true
Size: 2
Apple
Mango

Explanation: The program creates an ArrayList object using a Collection reference. It adds and removes elements, checks whether an element exists, retrieves the collection size, and traverses the elements using an enhanced for loop.

Hierarchy of Collection Interface

The Collection interface is part of a hierarchy that extends Iterable, which means collections can be traversed.

collection_interface
Hierarchy of Collection Interface

Note: The Map interface belongs to the Java Collections Framework but does not extend Collection.

Declaration of Collection Interface

The Collection interface extends the Iterable interface, allowing its elements to be traversed.

Syntax:

public interface Collection<E> extends Iterable<E>

Here, E represents the type of elements stored in the collection.

Object Creation:

Collection<String> fruits = new ArrayList<>();

Since Collection is an interface, it cannot be instantiated directly. Instead, an object of an implementing class such as ArrayList is created and assigned to a Collection reference.

Sub-Interfaces of Collection Interface

1. List Interface

The List interface represents an ordered collection that allows duplicate elements.

2. Set Interface

The Set interface represents a collection that does not allow duplicate elements.

3. SortedSet Interface

The SortedSet interface extends Set and maintains elements in sorted order.

  • Supports operations on ranges of elements.
  • TreeSet is a commonly used implementation.

4. NavigableSet Interface

The NavigableSet interface extends SortedSet and provides methods for finding nearby elements.

  • Supports methods such as lower(), floor(), ceiling(), and higher().
  • TreeSet implements NavigableSet.

5. Queue Interface

The Queue interface stores elements for processing in a particular order.

  • Many implementations follow FIFO (First-In-First-Out).
  • PriorityQueue processes elements according to priority rather than insertion order.

6. Deque Interface

The Deque interface extends Queue and supports inserting and removing elements from both ends.

  • Can be used as a queue or stack.
  • Common implementations include ArrayDeque and LinkedList.

Note: Java 21 introduced SequencedCollection and SequencedSet to represent collections with a defined encounter order. These interfaces are part of the current Collection hierarchy

Operations on Collection Objects

The Collection interface provides several methods for manipulating elements.

  • Adding elements: add() and addAll() insert elements into a collection.
  • Removing elements: remove(), removeAll(), and clear() remove elements.
  • Searching elements: contains() and containsAll() check whether elements exist.
  • Accessing elements: Collection does not provide index-based access; use an enhanced for loop or Iterator to traverse elements.

Iterating Over a Collection

Collection extends Iterable, allowing elements to be traversed using an enhanced for loop or Iterator.

Example: Iterating using an Iterator.

Java
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Geeks{
    public static void main(String[] args){
        Collection<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        Iterator<String> it = fruits.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }
}

Output
Apple
Banana

Explanation: The iterator() method returns an Iterator that traverses the collection using hasNext() and next().

Methods of Collection Interface

Method

Description

add(E e)Adds an element to the collection (optional).
addAll(Collection<? extends E> c)Adds all elements from another collection.
clear()Removes all elements.
contains(Object o)Checks if the collection contains the specified element.
Takes Object as a parameter because it uses the equals(Object obj) method to check equality.
containsAll(Collection<?> c)Checks if all elements exist.
remove(Object o)Removes the first occurrence of the specified element. Takes Object as a parameter because it uses equals(Object obj) to identify the element to remove.
removeAll(Collection<?> c)Removes all elements present in another collection.
retainAll(Collection<?> c)Retains only elements present in another collection.
removeIf(Predicate<? super E> filter)Removes elements satisfying a predicate.
size()Returns the number of elements.
isEmpty()Checks if the collection is empty.
iterator()Returns an iterator over the elements.
stream()Returns a sequential stream.
parallelStream()Returns a parallel stream.
toArray()Converts the collection to an array.
Comment