Collection Interface - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Friday, November 11, 2016

Collection Interface

Collection interface is the root of the collection hierarchy. A collection represent a group of objects known as its elements. Collection is a generic interface has declaration:

interface Collection<E>

Here, E specifies the type of objects that the collection will hold. Collection interface extends Iterable interface. This means that all collections can be iterate by use of for-each style for loop.

Method Description
int size() Returns the number of elements held in the invoking collection.
boolean isEmpty() Returns true if the invoking collection is empty. Otherwise, returns false.
boolean contains(Object obj) Returns true if the invoking collection contains the specified obj element. Otherwise, returns false.
Iterator iterator() Returns an iterator over the elements in invoking collection.
Object[] toArray() Returns an array containing all of the elements in invoking collection. The array elements are copies of the collection elements.
T[] toArray(T[] a) Returns an array that contains the elements of the invoking collection. The array elements are copies of the collection elements. If the size of array equals the number of elements, these are returned in array. If the size of array is less than the number of elements, a new array of the necessary size is allocated and returned. If the size of array is greater than the number of elements, the array element following the last collection element is set to null. An ArrayStoreException is thrown if any collection element has a type that is not a subtype of array.
boolean add(E e) Add e to the invoking collection. Return true if e was added to the collection. Return false if e is already a member of collection and the collection doesn't allow duplicates
boolean remove(Object e) Removes one instance of obj from the invoking collection. Returns true if the element was removed. Otherwise, returns false.
boolean containsAll(Collection<?> c) Returns true if the invoking collection contains all elements of c. Otherwise, returns false.
boolean addAll(Collection<? extends E> c) Adds all the elements of c to the invoking collection. Returns true if the collection changed (i.e., the elements were added). Otherwise, returns false.
boolean removeAll(Collection<?> c) Removes all elements of c from the invoking collection. Returns true if the collection changed (i.e., elements were removed). Otherwise, returns false.
default boolean removeIf( Predicate<? super E> predicate) Removes all of the elements of the invoking collection that satisfy the condition specified by predicate. (Added by JDK 8.)
boolean retainAll(Collection<?> c) Removes all elements from the invoking collection except those in c. Returns true if the collection changed (i.e., elements were removed). Otherwise, returns false.
void clear( ) Removes all elements from the invoking collection.
boolean equals(Object obj) Returns true if the invoking collection and obj are equal. Otherwise, returns false.
int hashCode( ) Returns the hash code for the invoking collection.
default Spliterator<E> spliterator( ) Returns a spliterator to the invoking collections. (Added by JDK 8.)
default Stream stream( ) Returns a sequential stream with invoking collection as its source for elements.(Added by JDK 8.)
default Stream<E> parallelStream( ) Returns a stream that uses the invoking collection as its source for elements. If possible, the stream supports parallel operations. (Added by JDK 8.)

No comments :

Post a Comment