Java Reference
In-Depth Information
Methods for array operations
Methods for comparison operations
Methods in the Collection interface are further classified as optional and required . An implementation class
is not required to provide an implementation for the optional methods. If an implementation class chooses not to
provide an implementation for optional methods, those methods must throw an UnsupportedOperationException .
Methods for Basic Operations
Methods for basic operations let you perform basic operations on a collection such as getting its size (number of
elements), adding a new element to it, removing an element from it, checking if an object is an element of this
collection, checking if the collection is empty, etc. Some of the methods in this category are as follows:
int size() : Returns the number of elements in the collection.
boolean isEmpty() : Returns true if the collection is empty. Otherwise, it returns false. This
acts the same as checking size() for 0.
boolean contains(Object o) : Returns true if the collection contains the specified object.
Otherwise, it returns false.
boolean add(E o) : Adds an element to the collection. It returns true if the collection changed.
Otherwise, it returns false. If the implementation does not allow duplicate elements in a
collection, this method will return false when you call it with an element that is already in the
collection. If a collection is size constrained and there is no space, the method throws a
java.lang.IllegalStateException .
boolean remove(Object o) : Removes the specified object from the collection. Returns true if
the collection changed because of this call. Otherwise, it returns false .
Iterator<E> iterator() : Returns an iterator that can be used to traverse elements in the
collection.
Methods for Bulk (or Group) Operations
Methods for bulk operations let you perform operations on a collection that involves a group of objects such as
removing all elements from it, checking if a collection contains all elements from another collection, adding
all elements of a collection to another collection, etc. Some of the methods in this category are as follows:
boolean addAll(Collection<? extends E> c) : Adds all elements of the specified collection
to this collection. Returns true if the collection changes because of this call. Otherwise,
it returns false.
void clear() : Removes all elements of the collection.
boolean containsAll(Collection<?> c) : Returns true if all the elements in the specified
collection are also elements of the collection. Otherwise, it returns false.
boolean removeAll(Collection<?> c) : Removes all elements from the collection that are
elements of the specified collection. Returns true if the collection changed as a result of this
call. Otherwise, it returns false.
boolean retainAll(Collection<?> c) : Retains only those elements that are also elements
of the specified collection. That is, it will remove all elements from the collection that are not
elements of the specified collection. Returns true if the collection changes as a result of this
call. Otherwise, it returns false.
 
Search WWH ::




Custom Search