The Java collection interface is defined in java.lang package. It extends the Iterable interface.
public interface Collection<E> extends Iterator<T>
where E represents the type of elements in collection.
The Collection framework is built upon the java collection interface because it declares some core methods that are implemented in all collections. It means the Collection interface is the interface that is implemented by all the classes in the collection framework.
Collection interface methods
Collection interface has 14 methods and it inherits one method from the Iterable interface. All the methods of collection interface have a different purpose and they are defined to perform different types of operations. We categorized these methods according to their use and purpose:
- Basic operations
- Bulk operations
- Comparison operations
- Array operations
- Aggregate operations
The collection interface declared some methods to perform basic operations like add, remove, size, etc. These operations are very useful for the programmer in the case of collection.
- add() method: This method is used to add the new element in the collection.
- remove() method: This method is used to remove a particular element for collection.
- size() method: This method is used to get the size of the collection. It returns the count of elements present in the collection.
- isEmpty() method: This method is used to check whether the collection is empty or not. It returns true if no element presents in collection else false.
- contains() method: This method is used to check whether the given elements exist in the collection or not. It returns true if the element presents in collection else false.
The collection interface declared some methods to operate in bulk. You can perform operations on a group of objects.
- addAll() method: This method is used to add all elements of a collection to another collection.
- removeAll() method: This method is used to remove all of an element from another collection.
- containsAll() method: This method is used to check whether all elements of a collection are present in another collection.
We can compare two collections by use of methods. We can use comparison operations to compare two collections for equality.
- equals() method: This method used to compare the specified object with other objects.
- hashCode() method: This method returns the hash code value of the collection.
The collection interface provides some methods that can convert a collection into an array. Each class that implements the collection interface must provide the implementation of method. Here we have two methods:
- toArray() method: This method returns the array of Objects. It can convert the collection to an array.
- toArray(T a) method: This method returns the array of T type. It can convert the collection to a T typed array.
By use of some method, you can perform aggregation in collections. The Collection interface supports streams that help to perform the aggregation. A stream is a sequence of elements that supports sequential and parallel aggregate operations. We can create a Stream object from a collection using the following methods from the Collection interface:
- stream() method: This method returns a stream with the collection as the source of elements. It returns a sequential Stream with this collection as its source.
- parallelStream() method: This method returns a possibly parallel Stream with this collection as its source. It is allowable for this method to return a sequential stream.