Collections in Java
Collections are any individual objects group that represents a single unit. The collection holds the collections of the classes and interfaces. It provides the architecture to store and manipulate data.
The standard method of collection is Array, Vector, and Hashtables.
Hierarchy of the Collection framework
The collection contains all the classes and interfaces that are required by the gathering framework. the gathering contains an interface named an iterable interface that permits iterate through all the collections. The interface is extended by the most collection interface which acts as a root for the gathering. Collections extend the gathering interface thereby extending the properties of the iterator and therefore the methods of this interface.

Methods of the Collection Interface
- add(Object): This method is used to add an object to the collection.
- addAll(Collection c): This method adds all the elements in the given collection to this collection.
- clear(): This method removes all of the elements from this collection.
- contains(Object o): This method returns true if the collection contains the specified element.
- equals(Object o): This method compares the specified object with this collection for equality.
- hashCode(): This method is used to return the hash code value for this collection.
- isEmpty(): This method returns true if this collection contains no element.
- iterator(): This method returns an iterator over elements in the collection.
- max(): This method is used to return the maximum value present in the collection.
- parallelStream(): This method returns a parallel Stream with this collection as its source.
- remove(Object o): This method is used to remove the given object from the collection. If there are duplicate values, then this method removes the first occurrence of the object.
- removeAll(Collection c): This method is used to remove all the objects mentioned in the given collection from the collection.
- removeIf(Predicate filter): This method is used to removes all the elements of this collection that satisfy the given predicate.
- retainAll(Collection c): This method is used to retains only the elements in this collection that are contained in the specified collection.
- size(): This method is used to return the number of elements in the collection.
- spliterator(): This method is used to create a Spliterator over the elements in this collection.
- stream(): This method is used to return a sequential Stream with this collection as its source.
- toArray(): This method is used to return an array containing all of the elements in this collection.
Interfaces that extend the Collections Interface
- Iterable Interface: This is the basic interface for the whole collection framework. The gathering interface extends the iterable interface. Therefore, inherently, all the interfaces and classes implement this interface. The most functionality of this interface is to supply an iterator for the collections.
Syntax: Iterator iterator();
2. Collection Interface: This interface extends the iterable interface and is implemented by all the classes within the collection framework. This interface contains all the essential methods which each collection has like adding the info into the gathering, removing the info, clearing the info, etc. of these methods are implemented during this interface because these methods are implemented by all the classes regardless of their sort of implementation. And also, having these methods during this interface ensures that the names of the methods are universal for all the collections.
3. List Interface: This interface is devoted to the info of the list type during which we will store all the ordered collections of the objects. This also allows duplicate data to be present in it.
List Interfaces are:
- ArrayList: ArrayList provides us dynamic array list. The dimensions of an ArrayList are increased automatically if the gathering grows or shrinks if the objects are far away from the gathering.
Syntax: List<T> al = new ArrayList<>(); - LinkedList: It is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses.
Syntax: List<T> al = new LinkedList<>(); - Vector: A vector provides us with dynamic arrays. Though it’s going to be slower than standard arrays in programs where many manipulations within the array are required. This is often just like ArrayList in terms of implementation. The difference between a vector and an ArrayList is that a Vector is synchronized and an ArrayList is non-synchronized.
Syntax: List<T> al = new Vector<>(); - Stack: Stack class models and implements the stack arrangement. the category is predicated on the essential principle of last-in-first-out. additionally, to the essential push and pop operations, the category provides three more functions of empty, search and peek.
Syntax: Stack<T> stack = new Stack<>();
Here, “ T” is the type of the object.
Example:

4. Queue Interface: A queue interface maintains the FIFO(First In First Out) order. This interface storing all the elements where the order of the elements matter.
- Priority Queue: PriorityQueue is employed when the objects are alleged to be processed supported the priority. The PriorityQueue is predicated on the priority heap. the weather of the priority queue is ordered consistent with the natural ordering.
Syntax: Queue<T> pq = new PriorityQueue<>();
Example:

5. Deque Interface: It is a double-ended queue and in the data structure where we can add and remove the elements from both ends of the queue from starting and ending.
Syntax: Deque<T> ad = new ArrayDeque<>();
Example:

6. Set Interface: A set is an unordered collection of objects during which duplicate values can’t be stored. This collection is employed once we wish to avoid the duplication of the objects and need to store only the unique objects.
- HashSet: HashSet data structures are not inserted data in the same manner as implementation. The objects are inserted based on their hashcode.
Syntax: Set<T> hs = new HashSet<>(); - LinkedHashSet: LinkedHashSet is similar to a HashSet. It is a doubly-linked list to store the data and retains the ordering of the elements.
Syntax: Set<T> lhs = new LinkedHashSet<>(); - TreeSet: The ordering of the weather is maintained by a group using their natural ordering whether or not a particular comparator is provided. This must be according to equals if it’s to properly implement the Set interface. It also can be ordered by a Comparator provided at set creation time, counting on which constructor is employed and sorting occurs as BST.
Syntax: Set<T> ts = new TreeSet<>();
Here, “T” is the type of the object.
Example:

7. Map Interface: Map supports the key-value pair for data and map doesn’t support duplicate keys because the same key cannot have multiple mappings.
- HashMap: HashMap provides the basic implementation of the Map and it stores data in key-value pairs.
Syntax: Map<T> hm = new HashMap<>(); - TreeMap: TreeMap is similar to HashMap. The difference between both of them is TreeMap stores the data in sorted order of key in mapping.
Map<T> tm = new TreeMap<>();
Here, “T” is the type of the object.
Example:
