Open In App

Collection Interface in Java

Last Updated : 03 Oct, 2025
Comments
Improve
Suggest changes
20 Likes
Like
Report

The Collection interface in Java is the root interface of the Java Collection Framework (JCF). It defines a group of objects known as elements and provides basic operations for working with them. The interface is part of the java.util package and forms the foundation for most collection classes, including List, Set, and Queue.

  • Defined in java.util package.
  • Root interface of all standard collections.
  • Not directly implemented. Instead, subclasses like ArrayList, HashSet, and LinkedList implement it.
  • Supports generic types, making collections type-safe.

Collection Interface Declaration

public interface Collection<E> extends Iterable<E>

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

Hierarchy of Collection Interface

The Collection interface is part of a hierarchy that extends Iterable, 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>

Implementing Classes

Some common classes implementing Collection or its subinterfaces:

  • List: ArrayList, LinkedList, Vector, Stack
  • Set: HashSet, LinkedHashSet, TreeSet, EnumSet, CopyOnWriteArraySet
  • Queue / Deque: PriorityQueue, ArrayDeque, LinkedBlockingQueue, ConcurrentLinkedQueue
  • Abstract classes: AbstractCollection, AbstractList, AbstractSet, AbstractQueue, AbstractSequentialList

Note: The Collection Interface is not limited to the above classes, there are many more classes.

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 an element exists.
containsAll(Collection<?> c)Checks if all elements exist.
remove(Object o)Removes a single instance of the specified element.
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.
equals(Object o)Compares this collection with another object.
hashCode()Returns hash code of the collection.
spliterator()Returns a Spliterator for elements.

From Iterable Interface:

MethodDescription
forEach(Consumer<? super T> action)Performs the given action for each element.

Java Collection Interface Examples

1. Adding Elements to a Collection 

The add(E e) and addAll(Collection c) methods provided by Collection can be used to add elements. 

Java
import java.util.*;

public class Geeks {
    public static void main(String[] args) {
        Collection<Integer> l1 = new ArrayList<>(5);
        l1.add(15); l1.add(20); l1.add(25);

        for (Integer n : l1) {
            System.out.println("Number = " + n);
        }

        Collection<Integer> l2 = new ArrayList<>();
        l2.addAll(l1);

        System.out.println("The new ArrayList is: " + l2);
    }
}

Output
Number = 15
Number = 20
Number = 25
The new ArrayList is: [15, 20, 25]

2. Removing Elements from a Collections

The remove(E e) and removeAll(Collection c) methods can be used to remove a particular element or a Collection of elements from a collection. 

Java
import java.util.*;

public class Geeks {
    public static void main(String[] argv) {
        Collection<Integer> hs1 = new HashSet<>(Arrays.asList(1,2,3,4,5));
        System.out.println("Initial set: " + hs1);

        hs1.remove(4);
        System.out.println("Set after removing 4: " + hs1);

        Collection<Integer> hs2 = new HashSet<>(Arrays.asList(1,2,3));
        hs1.removeAll(hs2);
        System.out.println("Set after removeAll(): " + hs1);
    }
}

Output
Initial set: [1, 2, 3, 4, 5]
Set after removing 4: [1, 2, 3, 5]
Set after removeAll(): [5]

3. 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() + " ");
        }
    }
}

Output
The list is: [Geeks, for, Geeks]
Iterator values: Geeks for Geeks 

Collection Interface in Java
Visit Course explore course icon
Article Tags :

Explore