Java Collections

In this tutorial, you will learn about collections framework in Java. You will also be introduced to different interfaces in the Java collections framework.

The collections framework in Java provides a set of classes and interfaces that can be used to organize and manipulate objects or data items.

The collections framework is considered as data structures in Java. It provides a standard way to store and manipulate data items and objects.

Note: A group of objects is considered as a collection.


Interfaces of Collections FrameWork

The Java collections framework provides various interfaces. These interfaces include several methods to perform different operations on collections.

Interfaces in the Java Collection Framework


Collections Framework Vs. Collection Interface

People often get confused between the Collections Framework and Collection Interface.

The Collection interface is one of the interfaces in the collections framework. The collections framework includes other interfaces as well: Map and Iterator. These interfaces may also have subinterfaces.


Java Collection Interface

The Collection interface is the root interface of the collections framework hierarchy.

Java does not provide direct implementations of the Collection interface but provides implementations of its subinterfaces like List, Set, and Queue.


Methods of Collection Interface

As mentioned above, the Collection interface includes various methods to perform different operations on data items.

add() Method

The add() method returns true if a new element is added to the collection. For example,

public interface Collection <E> {
   // returns true if a new element is added
   boolean add (E element);   
}

Note: Here, E indicates the type of elements in the collection.

size() Method

The size() method returns the number of elements in the collection. For example,

int size();

remove() Method

The remove() method removes an object from the collection. It returns true if the object is found and removed. For example,

// obj is an object to be removed
boolean remove(Object obj);  

iterator() Method

The iterator() method returns an iterator over the elements in the collection. For example,

Iterator<E> iterator();

addAll() Method

The addAll() method adds all the elements specified in a collection to our collection. It returns true if the elements are added. For example,

// elements from other collection is added to the current collection
boolean addAll(Collection<? extends E> other);

removeAll() Method

The removeAll() method removes all the elements specified in a collection from our collection. It returns true if the elements are removed. For example,

// elements of other collection are removed from the current collection
boolean removeAll(Collection<?> other);

clear() Method

The clear() method removes all the elements of the collection. For example,

void clear();

To learn about more methods of collection interface, visit Java Collection.


Subinterfaces of Collection

As mentioned earlier, the Collection interface includes subinterfaces that are implemented by Java classes.

All the methods of the Collection interface are also present in its subinterfaces.

Here are the subinterfaces of the Collection Interface:

Set Interface

The Set interface allows us to store elements in different sets similar to the set in mathematics. It cannot have duplicate elements.

List Interface

The List interface is an ordered collection that allows us to add and remove elements like an array.

Queue Interface

The Queue interface is used when we want to store and access elements in First In, First Out manner.


Java Iterator Interface

In Java, the Iterator interface provides methods that can be used to access elements of collections. For example,

public interface Iterator {
   E next();
   boolean hasNext();
   void remove();
}

Methods of Iterator

  • next() - returns the next element of the collection
  • hasNext() - returns true if there exists any element to be visited
  • remove() - removes the element returned by the next() method

Note: The remove() method is always used after the next() method. It is because the remove() method removes the element returned by next().

To learn more about the iterator interface, visit Java Iterator.


Java Map Interface

In Java, the Map interface allows elements to be stored in key-value pairs. For example,

Interface Map<K, V>

Here, K indicates the key and V indicates value. Keys are unique names that can be used to access a particular element in a map. And, each key has a single value associated with it.

Methods of Map

isEmpty() - returns true if the map is empty
  • size() - returns the number of key-value pairs in the map
  • get(Object key) - returns the value associated with the key
  • put(key, value) - store an element in map with corresponding key and value
  • isEmpty() - returns true if the map is empty

To learn about more methods of the map, visit Java Map.


The Java collections framework also provides implementations for all these interfaces. For example, the ArrayList class implements the list interface (a subinterface of the Collection Interface). The class implements all the methods included in the Collection interface.

We will learn about the collections framework and its implementation in different classes in detail in later tutorials.