Collections Framework – The Collection Interface

Collection Interface Overview

Collection Interface is root interface in the collection hierarchy. A Collection represents a group of objects known as its elements. List, Queue and Set are all sub interfaces of Collection interface. JDK does not provide any direct implementations of this interface. But, JDK provides direct implementations of it’s sub interfaces.
Here is the class diagram of Collection interface.
 From above class diagram, the Collection interface extends Iterable interface which is a member of java.lang package.  Implementing Iterable interface allows an object to be the target of the "for-each loop" statement.
Iterable interface contains below methods:
1. default void forEach(Consumer<? super T> action) - Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
2. Iterator<T> iterator() - Returns an iterator over elements of type T.
3. default Spliterator<T> spliterator() - Creates a Spliterator over the elements described by this Iterable.

Collection Interface Simple Example

Example: Let's say Collection of animals, which may be a List, a Set, or another kind of Collection. This idiom creates a new ArrayList (an implementation of the List interface), initially containing all the elements in animals list.
import java.util.ArrayList;
import java.util.Collection;

public class CreateCollectionInterfaceExample {

    public static void main(String[] args) {
        // Creating an ArrayList of String using 
     Collection<String> animals = new ArrayList<>();
        // Adding new elements to the ArrayList
        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Cat");
        animals.add("Dog");
        System.out.println(animals);
        
        String[] arrayOfAnimals = new String[4];
        System.out.println(arrayOfAnimals.length);
        arrayOfAnimals[0] = "Lion";
        arrayOfAnimals[1] = "Tiger";
        arrayOfAnimals[2] = "Cat";
        arrayOfAnimals[3] = "Dog";
        for (String string : arrayOfAnimals) {
         System.out.println(string);
 }
        
    }
}

Collection Interface Methods/APIs

  • boolean add(E e) - Ensures that this collection contains the specified element (optional operation).
  • boolean addAll(Collection<? extends E> c) - Adds all of the elements in the specified collection to this collection (optional operation).
  • void clear()- Removes all of the elements from this collection (optional operation).
  • boolean contains(Object o)- Returns true if this collection contains the specified element.
  • boolean containsAll(Collection<?> c)- Returns true if this collection contains all of the elements in the specified collection.
  • boolean equals(Object o)- Compares the specified object with this collection for equality.
  • int hashCode() - Returns the hash code value for this collection.
  • boolean isEmpty() - Returns true if this collection contains no elements.
  • Iterator iterator()- Returns an iterator over the elements in this collection.
  • default Stream parallelStream()- Returns a possibly parallel Stream with this collection as its source.
  • boolean remove(Object o)- Removes a single instance of the specified element from this collection, if it is present (optional operation).
  • boolean removeAll(Collection<?> c)- Removes all of this collection's elements that are also contained in the specified collection (optional operation).
  • default boolean removeIf(Predicate<? super E> filter)- Removes all of the elements of this collection that satisfy the given predicate.
  • boolean retainAll(Collection<?> c)- 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() - Creates a Spliterator over the elements in this collection.
  • default Stream stream() - Returns a sequential Stream with this collection as its source.
  • Object[] toArray() - Returns an array containing all of the elements in this collection.
  • T[] toArray(T[] a) - Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.
  1. The Collection interface perform basic operations, such as int size(), boolean isEmpty(), boolean contains(Object element), boolean add(E element), boolean remove(Object element), and Iterator iterator().
  2. It also contains methods that operate on entire collections, such as boolean containsAll(Collection> c), boolean addAll(Collection extends E> c), boolean removeAll(Collection> c), boolean retainAll(Collection<?> c), and void clear().
  3. Additional methods for array operations (such as Object[] toArray() and T[] toArray(T[] a) exist as well.
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. (See the lesson entitled Aggregate Operations for more information about using streams.)

Comments