Collection in Java
We have the array in Java to group similar types of elements, but the problem with the array is that the size of the array is fixed and It can hold similar types of elements.—To overcome these issues with the array, Java introduces collection in JDK 1.2.
Java collection is used to represent a group of the object as a single entity. Java provides Collection Framework which defines several interfaces like List, Set, Queue, and classes like ArrayList, HashSet, and PriorityQueue to group the objects as a single unit.
Collection framework provides several classes and interfaces which can be used as a group of the object as a single entity. The Hierarchy of Interfaces and class in the collection framework shown below.
All the classes implement the collection interface in the collection framework. Collection interface has many methods which are implemented in every class of collection framework. Collection interface provides methods to perform operations like sorting, searching, insertion and deletion, etc. It is the root interface of the collection framework.
List of methods in Collection Interface
|1||public boolean add(E e)||It is used to insert an element in this collection.|
|2||public boolean addAll(Collection<? extends E> c)||It is used to insert the specified collection elements in the invoking collection.|
|3||public boolean remove(Object element)||It is used to delete an element from the collection.|
|4||public boolean removeAll(Collection<?> c)||It is used to delete all the elements of the specified collection from the invoking collection.|
|5||default boolean removeIf(Predicate<? super E> filter)||It is used to delete all the elements of the collection that satisfy the specified predicate.|
|6||public boolean retainAll(Collection<?> c)||It is used to delete all the elements of invoking collection except the specified collection.|
|7||public int size()||It returns the total number of elements in the collection.|
|8||public void clear()||It removes the total number of elements from the collection.|
|9||public boolean contains(Object element)||It is used to search for an element.|
|10||public boolean containsAll(Collection<?> c)||It is used to search the specified collection in the collection.|
|11||public Iterator iterator()||It returns an iterator.|
|12||public Object toArray()||It converts the collection into the array.|
|13||public T toArray(T a)||It converts the collection into the array. Here, the runtime type of the returned array is that of the specified array.|
|14||public boolean isEmpty()||It checks if the collection is empty.|
|15||default Stream parallelStream()||It returns a possibly parallel Stream with the collection as its source.|
|16||default Stream stream()||It returns a sequential Stream with the collection as its source.|
|17||default Spliterator spliterator()||It generates a Spliterator over the specified elements in the collection.|
|18||public boolean equals(Object element)||It matches two collections.|
|19||public int hashCode()||It returns the hash code number of the collection.|
List is a child interface of Collection.
⇒If we want to represent a group of the object as a single entity where duplicates are allowed and insertion order should be preserved then we should go for List.
⇒We can differentiate duplicate by using the index.
⇒ArrayList, LinkedList, Vector classes implement List Interface.
Set is the child interface of Collection.
⇒If we want to represent a group of individual objects as a single entity where duplicates are not allowed and insertion order not preserved then we should go for Set.
⇒Set Interface does not contain any new method, we have to use only collection interface methods.
⇒HashSet, LinkedHashSet, and SortedSet implements the Set interface.
Queue is also a child interface of collection.
⇒Queue interface maintains the first-in-first-out order.
⇒It can be defined as an ordered list that is used to hold the elements which are about to be processed.
⇒There are various classes like PriorityQueue, Deque, and ArrayDeque which implements the Queue interface.