Collections Framework – The Collection Interface

In this guide, we will learn about the Collection interface of the Java Collections framework.

Here are the key points about Collection interface:
  • If we want to represent a group of individual objects as a single entity then we should go for collections.
  • Collection interface is considered as the root interface of the Collection Framework.
  • Collection interface defines the most common methods which are applicable for any collection object.
  • List, Queue, and Set are all subinterfaces of Collection interface. 
  • JDK does not provide any direct implementations of this interface. But, JDK provides direct implementations of it’s sub-interfaces.

Collection Interface Class Diagram

Here is the class diagram of the Collection interface.
From the above class diagram, the Collection interface extends the Iterable interface which is a member of the java.lang package.  Implementing the Iterable interface allows an object to be the target of the "for-each loop" statement.

Collection Interface Methods/APIs

  • boolean add(E e) - This method ensures that this collection contains the specified element (optional operation).
  • boolean addAll(Collection<? extends E> c) - This method adds all of the elements in the specified collection to this collection (optional operation).
  • void clear()- This method removes all of the elements from this collection (optional operation).
  • boolean contains(Object o) - This method returns true if this collection contains the specified element.
  • boolean containsAll(Collection<?> c) - This method returns true if this collection contains all of the elements in the specified collection.
  • boolean equals(Object o)- This method compares the specified object with this collection for equality.
  • int hashCode() - This method returns the hash code value for this collection.
  • boolean isEmpty() - This method returns true if this collection contains no elements.
  • Iterator iterator() - This method returns an iterator over the elements in this collection.
  • default Stream parallelStream() - This method returns a possibly parallel Stream with this collection as its source.
  • boolean remove(Object o)-  This method removes a single instance of the specified element from this collection, if it is present (optional operation).
  • boolean removeAll(Collection<?> c) - This method removes all of this collection's elements that are also contained in the specified collection (optional operation).
  • default boolean removeIf(Predicate<? super E> filter) - This method removes all of the elements of this collection that satisfy the given predicate.
  • boolean retainAll(Collection<?> c) - This method retains only the elements in this collection that are contained in the specified collection (optional operation).
  • int size() - Returns the number of elements in this collection.
  • default Spliterator spliterator() - This method creates a Spliterator over the elements in this collection.
  • default Stream stream() - This method returns a sequential Stream with this collection as its source.
  • Object[] toArray() - This method returns an array containing all of the elements in this collection.
  • T[] toArray(T[] a) - This method returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

Collection Interface Example

The following example demonstrates the usage of important Collection interface methods:
package com.java.collections.interfaces;

import java.util.ArrayList;
import java.util.Collection;

public class CollectionDemo {

    public static void main(String[] args) {

        Collection < String > fruitCollection = new ArrayList < > ();
        fruitCollection.add("banana");
        fruitCollection.add("apple");
        fruitCollection.add("mango");
        System.out.println(fruitCollection);

        fruitCollection.remove("banana");
        System.out.println(fruitCollection);

        System.out.println(fruitCollection.contains("apple"));

        fruitCollection.forEach((element) -> {
            System.out.println(element);
        });

        fruitCollection.clear();

        System.out.println(fruitCollection);
    }
}
Output
[banana, apple, mango]
[apple, mango]
true
apple
mango
[]
In JDK 8 and later, the Collection interface also exposes methods Stream stream() and Stream parallelStream(), for obtaining sequential or parallel streams from the underlying collection. 

This is demonstrated in the below posts:

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments

  1. syntax error in CollectionDemo class as "element cannot be resolved to a variable"
    Please check once.

    ReplyDelete
  2. this can be used in place of for each :-

    for (String fruit : fruitCollection) {
    System.out.println(fruit);
    }

    ReplyDelete

Post a Comment