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.
Interface | Description |
---|---|
The Collection Interface | It enables you to work with groups of objects. |
The List | It is used to store an ordered collection of elements. |
The Set | It is use to handle sets, which must contain unique elements. |
The SortedSet | It is used to handle sorted sets. |
The Map | This maps unique keys to values. |
The Map.Entry | It describes an element (a key/value pair) in a map. It is an inner class of Map. |
The SortedMap | This extends Map so that the keys are maintained in shorted order. |
The Enumeration | This 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. |
The collection interface contains various methods which is used to perform different Operations.
Methods | Description |
---|---|
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. |