Last Updated: 06 March, 2023
The Java Collection interface was introduced in JDK 1.2 and is available in the java.util package.
The Collection interface is one of the root interfaces of the collection framework hierarchy. The Collection interface is not directly implemented by any collection framework classes such as ArrayList, HashSet, etc.; it is implemented through its subinterfaces such as List, Set, and Queue.
The Collection interface defines all commonly used methods, which are used by almost all the collections. Some collections allow duplicate elements, and others do not. Some are ordered and others unordered.
As we can see in the above Collection interface hierarchy, Collection is a public interface that extends only the Iterable Interface in Java.
Collection Interface declaration:
Here, E defines the type of elements that the Set will contain.
There are the following sub-interfaces of the Collection Interface:
|
|
|
All the Collection Framework classes implement the subinterfaces of the Collection interface and access all the defined methods of their subinterfaces as well as the Collection interface.
The Collection interface is implemented by the following classes:
|
|
Output
Collection Elements: [India, Canada, US, England]
Size of Collection: 4
Contains: true
Collection Elements after remove: [India, Canada, US]
Is Collection is empty: true
Methods | Description |
boolean add(E e) | Ensures that this collection contains the specified element (optional operation). |
boolean addAll(Collection<? extends E> c) | All elements from the specified collection are added to this collection (optional operation). |
void clear() | Removes all the elements from this collection (optional operation). |
contains(Object o) | This method returns true if this collection contains the specified element. |
boolean containsAll(Collection<?> c) | Returns true if this collection contains all the elements in the specified collection. |
boolean equals(Object o) | Compares the specified object with this collection for equality. |
int hashCode() | Returns the hash code value for this collection. |
boolean isEmpty() | Returns true if this collection contains no elements. |
Iterator<E> iterator() | Returns an iterator over the elements in this collection. |
default Stream<E> parallelStream() | Returns a possibly parallel stream with this collection as its source. |
boolean remove(Object o) | Removes a single instance of the specified element from this collection, if it is present (optional operation). |
boolean removeAll(Collection<?> c) | Removes all of this collection’s elements that are also contained in the specified collection (optional operation). |
default boolean removeIf(Predicate<? super E> filter) | Removes all the elements of this collection that satisfy the given predicate. |
boolean retainAll(Collection<?> c) | Retains only the elements in this collection that are contained in the specified collection (optional operation). |
default Spliterator<E> spliterator() | Creates a Spliterator over the elements in this collection. |
default Stream<E> stream() | Returns a sequential Stream with this collection as its source. |
Object[] toArray() | Returns an array containing all of the elements in this collection. |
<T> T[] toArray(T[] a) | Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
default | Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array. |
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com