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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…