πŸŽ‰ Festival Deal: Save Big on Professional Courses with Real Projects – Apply Coupons FEST300OFF FEST500OFF
β†’ Click Here ←

Collection Interface in Java  


Introduction
  • Collection Interface is the root interface of the Collections Framework in Java.
  • It is present in the java.util package.
  • It was introduced in JDK 1.2 version.
  • Hierarchy of Collection Interface:
    • Collection Interface Hierarchy in Java
  • It represents a group of objects, known as elements.
  • It defines the most common methods that every collection class (like List, Queue, Set) should implement.
  • Syntax :
    public interface Collection<E> extends Iterable<E>
    {
        // methods (abstract and default)
    }
Methods of Collection Interface:
  • Below are the methods defined specifically in the Collection interface:
Sr. No. Method Description
1 boolean add(E e) Adds a single element to the collection. Returns true if the collection changed.
2 boolean addAll(Collection<? extends E> c) Adds all elements from another collection. Returns true if the collection changed.
3 boolean contains(Object o) Checks if the collection contains the specified element. Returns true if found.
4 boolean isEmpty() Checks if the collection has no elements. Returns true if empty.
5 boolean remove(Object o) Removes a single instance of the specified element. Returns true if removed.
6 void clear() Removes all elements from the collection, making it empty.
7 int size() Returns the number of elements in the collection.
8 Iterator<E> iterator() Returns an Iterator to traverse elements one by one.
9 Object[] toArray() Converts the collection into an array of Object.
10 <T> T[] toArray(T[] a) Converts the collection into an array of the specified type.
11 boolean containsAll(Collection<?> c) Checks if the collection contains all elements of the specified collection.
12 boolean removeAll(Collection<?> c) Removes all elements in this collection that are also contained in the specified collection.
13 boolean retainAll(Collection<?> c) Retains only the elements in this collection that are also contained in the specified collection.
14 default boolean removeIf(Predicate<? super E> filter) Removes all elements that satisfy the given predicate. (Java 8+)
15 default Stream<E> stream() Returns a sequential Stream with this collection as the source. (Java 8+)
16 default Stream<E> parallelStream() Returns a possibly parallel Stream with this collection as the source. (Java 8+)
17 default Spliterator<E> spliterator() Creates a Spliterator over the elements in this collection. (Java 8+)
Program:
  • Collection simple generic program that works with any type of collection (List, Set, Queue).
    import java.util.*;
    
    public class CollectionDemo
    {
        public static void printCollection(Collection<String> collection)
        {
            for (String element : collection)
            {
                System.out.println(element);
            }
        }
    
        public static void main(String[] args)
        {
            Collection<String> list = new ArrayList<>();
            list.add("Amit");
            list.add("Deepak");
            printCollection(list);
    
            System.out.println("-----------");
    
            Collection<String> set = new HashSet<>();
            set.add("Kamal");
            set.add("Rahul");
            printCollection(set);
        }
    }
Use of Collection Interface:
  • Maximum Flexibility:
    • Use a Collection reference when we want our code should work with any type of collection (List, Set, or Queue) and we don't need special features like indexed access.
  • Easy to Maintain:
    • If a method only needs basic operations like add(), remove(), or iterate, using Collection makes it easy to switch between different collection types later.
  • Used in APIs:
    • Many libraries and frameworks use Collection in their APIs so that developers can pass any kind of collection without changing their code.
Properties of Collection Interface:
  • It is the root interface in the collection hierarchy.
  • It is a generic interface β€” Collection<E> (E represents the type of elements).
  • All major collection classes (like ArrayList, HashSet, LinkedList) implement it, directly or indirectly.
  • It provides basic operations like add, remove, iterate, size etc. which are common for all collections.
  • It can store both homogeneous and heterogeneous elements (but to ensure type-safety we should use generics).