Java Collections

Collection in Java represents a group of objects. It is simply an object that contains other objects.

Collections in Java – Collection Framework

The Collection framework in Java provides efficient implementations of essential data structures.  It provides an architecture to store and manipulate objects inside the collection/group.

The Collection framework contains interfaces and classes that we can use to write programs for manipulating objects within a group.

Most used Collection interfaces:

  • Collection
  • List
  • Set
  • Map
  • Queue
  • Deque

Most used Collection classes are implementations of List, Set, and Map interfaces:

Hierarchy of Collection Framework

Note: Map is not included since Map does not extend the Collection interface.

collections java


Iterable Interface

The Iterable interface gives us the ability to iterate through the elements of a collection. This means a class that implements the Java Iterable interface can have its elements iterated. It contains only one abstract method.

 Iterator<T> iterator()

The iterator() method returns the Iterator of type T.

Iterator Interface

An Iterator is an object that can be used to loop through collections.

The Iterator Interface has three methods:

  Method Description
1 public boolean hasNext() It returns true if the iterator has more elements otherwise it returns false.
2 public Object next() It returns the element and moves the cursor pointer to the next element.
3 public void remove() It removes the last elements returned by the iterator. It is less used.


Collection Interface

All collections except Maps inherit the Collection interface. The Collection interface provides many useful methods for working with collections in Java.


Methods of the Collection Interface

  Method Description
  public boolean add(E e) It is used to insert an element in this collection.
  public boolean addAll(Collection<? extends E> c) It is used to insert the specified collection elements in the invoking collection.
  public boolean remove(Object element) It is used to delete an element from the collection.
  public boolean removeAll(Collection<?> c) It is used to delete all the elements of the specified collection from the invoking collection.
  default boolean removeIf(Predicate<? super E> filter) It is used to delete all the elements of the collection that satisfy the specified predicate.
  public boolean retainAll(Collection<?> c) It is used to delete all the elements of invoking collection except the specified collection.
  public int size() It returns the total number of elements in the collection.
  public void clear() It removes the total number of elements from the collection.
  public boolean contains(Object element) It is used to search an element.
  public boolean containsAll(Collection<?> c) It is used to search the specified collection in the collection.
  public Iterator iterator() It returns an iterator.
  public Object[] toArray() It converts collection into array.
  public <T> T[] toArray(T[] a) It converts collection into array. Here, the runtime type of the returned array is that of the specified array.
  public boolean isEmpty() It checks if collection is empty.
  default Stream<E> parallelStream() It returns a possibly parallel Stream with the collection as its source.
  default Stream<E> stream() It returns a sequential Stream with the collection as its source.
  default Spliterator<E> spliterator() It generates a Spliterator over the specified elements in the collection.
  public boolean equals(Object element) It matches two collections.
  public int hashCode() It returns the hash code number of the collection.


This was an introduction to Collections in Java. Proceed to the next lesson to learn more about the Java ArrayList class.

Happy Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *