What is a collection in Java?
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:
- Interfaces and their class implementations
- algorithms
The two primary “root” interfaces of Java collection classes are Collection (java.util.Collection) and Map (java.util.Map).
Hierarchy of Collection Framework:
All the classes and interfaces for the Collection framework are included in java.util package.
Advantages of Collection Framework:
- Multiple ad hoc collecting APIs are not necessary for us to learn.
- It offers methods to control them, as well as a common interface for collections that promotes software reuse.
- Eliminates the need to create ad hoc collections APIs, which lowers the effort needed to design and implement APIs.
- It offers helpful data structures and algorithms that require less programming work than writing them from scratch.
- It offers efficient implementations of practical data structures and algorithms to boost performance.
- Helps create a shared language for exchanging collections between APIs that are not related to one another, enabling interoperability.
- Collection can expand and can be resized.
Disadvantages of collections framework:
- It must cast to the correct type.
- It can’t be done compile-time type checking.
Methods of the Collection Interface:
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. |
Iterator interface
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
Follow Me
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.
Leave a Comment