In java “Collections” helps us to store and manipulate the collection of objects. All the operations such as manipulation, deletion, searching, sorting, insertion can be performed quickly with the help of “Collections”. Java Collection Framework provides set of interfaces, classes and algorithms like sorting and searching.
Interface in collection API:
Collection Interface declares some methods which are contained by all the collections. It is good to have understanding of all the methods in the Collection Interface. The exception that are thrown by the methods of Collection is “UnsupportedOperationException”. Some of the interface in collection API are as below:
1- Collection : Hierarchically “Collection” interface is on top of other collection interfaces. This
interface enables us to work with collection of object.
2- Deque : Also pronounced as deck. It defines methods that helps in accessing the elements
from the start or end of Deque instance. It is a subtype of Queue.
3- List : It is a subtype Collection interface. It shows an ordered list of objects that enables find
elements in specific order.
4- Queue : It is subtype of Collection interface. Like List it also represents and ordered list of
objects. In Queue elements are added at the end of the queue and removed from the
start of the queue.
5- Set : It is a set of objects hence it contains unique objects i.e. no duplicates. It is a subtype
of Collection interface.
6- SortedSet : It is a subtype of java.util.Set interface. In SortedSet the returned elements are in
the sorted order default order is ascending order.
Below are interface methods and their description:
boolean addAll(Collection coll): This method is used to add all the elements of coll to the collection that invokes. It's return type is also boolean and if elements are added it will return true else false.
boolean add(Object objt): This method is used to add object i.e. objt to the collection that invokes. It's return type is boolean and false will indicate that the object is already a member of the collection hence duplicates are not allowed.
void clear(): The clear method is used to remove all the elements from the collection that invoked the method.
boolean containsAll(Collection coll): This method checks if Collection that invoked the method “containsAll” contains all the elements, if yes it returns true else false.
boolean contains(Object objt): The contains method checks if Object i.e. objt is an element of the collection that invoked the contains method if yes it returns true else false.
boolean equals(Object objt): This method returns true if the collection that invoked the method equals() and the objt are equal else it will return false.
int hasCode() : The method returns the hash code of the collection that invoked the method.
boolean isEmpty(): This method checks if the collection that invoked the method “isEmpty()” is empty or not if empty it returns true else false.
Iterator iterator(): The iterator method returns an iterator for the collection that invoked the method “interator()”.
boolean remove(Object objt): This remove a an instance of "objt" from the collection that invoked the method, remove. It returns ture if the instance is removed else false.
boolean removeAll(Collection coll): This method returns true if all the elements of "coll" are removed from the collection that invoked the method “removeAll()” else it returns false.
boolean retainAll(Collection coll): This method returns true if all the elements of collection that invoked the method “retainAll()” are removed except those in coll else it returns false.
Int size() : The size method gives the number of elements in the collection that invoked the method "size()" as a return value.
Object toArray(): In this method we get an array as a return value that contains all elements same as in the collection that invoked the method “toArray”.
Object toArray(Object objt): This method with one parameter returns an array that contains elements that matches with the elements of array in the context of type.