Java Collection interface is the root interface of the Java Collection Framework that represents a group of objects, known as elements.
In this chapter, you will learn about the Collection interface, its purpose, and how it is used to work with groups of objects in Java.
Collection is a group of objects, which are known as elements. It is the root interface in the collection hierarchy. This interface is mainly used to pass collections and perform operations on them where maximum generality is required.
The following syntax shows the declaration of the Collection interface:
The Collection interface does not have any constructors because it is an interface. Constructors are provided by the classes that implement this interface.
The Collection interface provides various methods to perform operations on a group of elements such as adding, removing, and checking elements.
The following table lists the commonly used methods of the Collection interface along with their descriptions.
| Method | Description |
|---|---|
| add() | This method returns a Boolean value true if it inserts the specified element in this collection. |
| addAll() | This method returns a Boolean value true if it adds all the elements of specified collection in the invoking collection. |
| clear() | It removes all the elements automatically from this collection. |
| contains() | It returns a Boolean value true if this queue contains the specified element. |
| containsAll() | It returns a Boolean value true if this collection contains all the elements in the specified collection. |
| equals() | This method returns a boolean value true if the specified object is equal with this collection. |
| hashCode() | It returns a hash code value for this collection. |
| isEmpty() | This method returns true if this collection contains no elements or is empty. |
| iterator() | It returns an iterator over the elements in proper sequence. |
| remove() | It removes the specified element from this queue, if it is present in the collection. |
| removeAll() | It removes all the elements of this collection which are also present in the specified collection. |
| removeIf() | It removes all the elements of this collection that satisfy the given predicate filter. |
| retainAll() | This method retains only those elements in this collection that are present in the specified collection. |
| size() | It returns the total number of the elements in this collection. |
| spliterator() | It returns a spliterator over the elements in this collection. |
| toArray() | It returns an array containing all the elements of this collection which are in proper sequence. |
Here are the examples of the Collection interface that demonstrate how to perform operations like adding, removing, iterating, and checking elements.
This example demonstrates how to add elements, check size, verify existence, and clear a collection.
Output:
Initial collection :[2, 5] Final Collection : [2, 5, 11, 12, 13, 14, 15] Size of Collection : 7 5 is present in the collection Elements in collection : []
This example demonstrates how to iterate over a collection and filter elements using the retainAll() method.
Output:
Collection : [11, 12, 13, 14, 15] 11 12 13 14 15 Queue is empty Elements in the set : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Multiple of 5 : [5, 10, 15, 20]
We request you to subscribe our newsletter for upcoming updates.