Java collection interface

The Java collection interface is defined in java.lang package. It extends the Iterable interface.

public interface Collection<E> extends Iterator<T> 

where E represents the type of elements in collection.

The Collection framework is built upon the java collection interface because it declares some core methods that are implemented in all collections. It means the Collection interface is the interface that is implemented by all the classes in the collection framework.

Java collection interface

Collection interface methods

Collection interface has 14 methods and it inherits one method from the Iterable interface. All the methods of collection interface have a different purpose and they are defined to perform different types of operations. We categorized these methods according to their use and purpose:

  • Basic operations
  • Bulk operations
  • Comparison operations
  • Array operations
  • Aggregate operations

Basic operations

The collection interface declared some methods to perform basic operations like add, remove, size, etc. These operations are very useful for the programmer in the case of collection.

  1. add() method: This method is used to add the new element in the collection.
  2. remove() method: This method is used to remove a particular element for collection.
  3. size() method: This method is used to get the size of the collection. It returns the count of elements present in the collection.
  4. isEmpty() method: This method is used to check whether the collection is empty or not. It returns true if no element presents in collection else false.
  5. contains() method: This method is used to check whether the given elements exist in the collection or not. It returns true if the element presents in collection else false.

Bulk operations

The collection interface declared some methods to operate in bulk. You can perform operations on a group of objects.

  1. addAll() method: This method is used to add all elements of a collection to another collection.
  2. removeAll() method: This method is used to remove all of an element from another collection.
  3. containsAll() method: This method is used to check whether all elements of a collection are present in another collection.

Comparison operations

We can compare two collections by use of methods. We can use comparison operations to compare two collections for equality.

  1. equals() method: This method used to compare the specified object with other objects.
  2. hashCode() method: This method returns the hash code value of the collection.

Array operations

The collection interface provides some methods that can convert a collection into an array. Each class that implements the collection interface must provide the implementation of method. Here we have two methods:

  1. toArray() method: This method returns the array of Objects. It can convert the collection to an array.
  2. toArray(T[] a) method: This method returns the array of T type. It can convert the collection to a T typed array.

Aggregate Operations

By use of some method, you can perform aggregation in collections. The  Collection interface supports streams that help to perform the aggregation. A stream is a sequence of elements that supports sequential and parallel aggregate operations. We can create a Stream object from a collection using the following methods from the Collection interface:

  1. stream() method: This method returns a stream with the collection as the source of elements. It returns a sequential Stream with this collection as its source.
  2. parallelStream() method: This method returns a possibly parallel Stream with this collection as its source. It is allowable for this method to return a sequential stream.

Leave a Comment