Java’s Collection framework offers an architecture for storing and managing a collection of objects. All data actions, including searching, sorting, insertion, manipulation, and deletion, are possible with Java Collections.
What is the Collection Framework?
The Collection framework is an integrated architecture for managing a collection of things. It has:
The two primary “root” interfaces of Java collection classes are Collection (java.util.Collection) and Map (java.util.Map).
All the classes and interfaces for the Collection framework are included in java.util package.
All the collections that implement this interface can directly use a variety of methods that are included in it. As follows:
Method | Description |
---|---|
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 every component from this collection. |
contains(Object o) | If the collection includes the requested element, this method returns true. |
containsAll(Collection c) | If the collection contains every element in the supplied collection, this method returns true. |
equals(Object o) | This method checks for equality between the provided item and this collection. |
hashCode() | The hash code value for this collection is returned using this method. |
isEmpty() | This method returns true if this collection contains no elements. |
iterator() | This method returns an iterator over the elements in this 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) | The provided object is removed from the collection using this function. This function eliminates the first instance of the object if there are duplicate values. |
removeAll(Collection c) | The objects listed in the provided collection are all removed from the collection using this function. |
removeIf(Predicate filter) | Using this technique, all collection elements that meet the stated predicate are removed. |
retainAll(Collection c) | Only the elements in this collection that are contained in the given collection are kept using this approach. |
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 returns a sequential Stream with this collection as its source. |
toArray() | This method returns an array containing all of the elements in this collection. |
The ability to iterate the elements just forward is provided by the iterator interface.
Interface methods for iterators: The Iterator interface only has three methods. As follows:
Method | Description |
---|---|
public boolean hasNext() | If the iterator has more elements, it returns true; otherwise, it returns false. |
public Object next() | The element is returned, and the cursor is then pointed at the subsequent element. |
public void remove() | The final elements that the iterator returned are deleted. It is less used. |
More on it will be revealed in the upcoming topics.
Note: also read about the Generics in Java
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given two singly linked lists that intersect at some node. Your task is…
A builder plans to construct N houses in a row, where each house can be…
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…