The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that are performed on a data such as searching, sorting, insertion, manipulation, and deletion. Java Collection means a single unit of objects. Java Collection framework provides many interfaces such as Set, List, Queue, Deque and classes such as ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet.
What is Collection
A Collection represents a single unit of objects, i.e., a group.
What is Collection framework
The Collection framework represents a unified architecture for storing and manipulating a group of objects. It has:
Interfaces and its implementations, i.e., classes
Iterator interface provides the facility of iterating the elements in a forward direction only.
Methods of Iterator interface
There are only three methods in the Iterator interface. They are described below
|public boolean hasNext()||It returns true if the iterator has more elements otherwise it returns false.|
|public Object next()||It returns the element and moves the cursor pointer to the next element.|
|public void remove()||It removes the last elements returned by the iterator. It is less used.|
The Iterable interface is the root interface for all the collection classes. The Collection interface extends the Iterable interface and therefore all the subclasses of Collection interface also implement the Iterable interface.
It contains only one abstract method. i.e.,
It returns the iterator over the elements of type T.
The Collection interface is the interface which is implemented by all the classes in the collection framework. It declares the methods that every collection will have. In other words, we can say that the Collection interface builds the foundation on which the collection framework depends.
Some of the methods of the Collection interface are
Boolean add (Object obj),
Boolean addAll (Collection c),
void clear(), etc. which are implemented by all the subclasses of the Collection interface.
Methods Of Collection Interface
|public boolean add(E e)||It is used to insert an element in this collection.|
|public boolean addAll(Collection<? extends E> c)||It is used to insert the specified collection elements in the invoking collection.|
|public boolean remove(Object element)||It is used to delete an element from the collection.|
|public boolean removeAll(Collection c)||It is used to delete all the elements of the specified collection from the invoking collection.|
|default boolean removeIf(Predicate<? super E> filter)||It is used to delete all the elements of the collection that satisfy the specified predicate.|
|public boolean retainAll(Collection c)||It is used to delete all the elements of invoking collection except the specified collection.|
|public int size()||It returns the total number of elements in the collection.|
|public void clear()||It removes the total number of elements from the collection.|
|public boolean contains(Object element)||It is used to search an element.|
|public boolean containsAll(Collection c)||It is used to search the specified collection in the collection.|
|public boolean isEmpty()||It checks if collection is empty.|