Table of content

Collection Interface

The primary interface of the collections framework is the Collection interface, Collection Interface provides the rules for all the collection subtypes should behave, here behavior is nothing but the methods to be present in the subclasses and sub-interfaces.

All the classes present in the Collection Framework must implement these functions, but they can also have their own methods. The logic inside these overridden methods could be different based on the classes.

Below are a few methods present in the collection interface,

  • boolean add(E e) adds the given element to the collection
  • boolean addAll (Collection<? extendsE> c) Adds all of the elements in the specified collection to this collection (optional operation).
  • void clear() Removes all of the elements from this collection (optional operation).
  • boolean contains(Object o) Returns true if this collection contains the specified element.
  • boolean containsAll (Collection<?> c) Returns true if this collection contains all of 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.
  • 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<? superE> filter) Removes all of 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).
  • int size() Returns the number of elements present in this collection.
  • defaultSpliterator<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.
  • default <T> T[] toArray (IntFunction<T[]> generator) Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.

java-collection-framework

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions