dsa

Java Collection Interfaces

In this tutorial, we will learn about the Java Collection interface and its subinterfaces.

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

The Collection interface is the foundation upon which the collections framework is built. It declares the core methods that all collections will have. These methods are summarized in the following table.

Java Collection Interface
Java Collection Interface

Sub-Interfaces of Collection Interface

InterfaceDescription
The Collection InterfaceIt enables you to work with groups of objects.
The ListIt is used to store an ordered collection of elements.
The SetIt is use to handle sets, which must contain unique elements.
The SortedSetIt is used to handle sorted sets.
The MapThis maps unique keys to values.
The Map.EntryIt describes an element (a key/value pair) in a map. It is an inner class of Map.
The SortedMapThis extends Map so that the keys are maintained in shorted order.
The EnumerationThis is legacy interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects. This legacy interface has been superceded by Iterator.

Methods of Collection Interface

The collection interface contains various methods which is used to perform different Operations.

MethodsDescription
add()It is used to insert the given element to the collection.
size()It returns the size of the collection.
remove()It helps to remove the given element from the collection.
iterator()It returns an iterator to access elements of the collection.
addAll()It adds all the elements of a given collection to the collection.
removeAll()It removes all the elements of the given collection from the collection.
clear()It removes all the elements of the collection.