In this article, you will learn-
Java Collections Framework
In this tutorial, we will find out about various interfaces of the Java collections framework.
The Java collections framework provides a set of interfaces and classes to execute different information structures and algorithms.
For instance, the LinkedList class of the assortments system gives the usage of the doubly-connected rundown data structure.
Interfaces of Collections FrameWork
The Java assortments system gives different interfaces. These interfaces remember a few techniques to perform various tasks for assortments.
We will find out about these interfaces, their subinterfaces, and execution in different classes in detail in the later chapters. Let’s learn about the commonly used interfaces in brief in this tutorial.
Java Collection Interface
The Collection interface is the root interface of the collections framework hierarchy.
Java does not provide direct implementations of the Collection interface but provides implementations of its subinterfaces like List, Set, and Queue. To learn more, visit: Java Collection Interface
Collections Framework Vs. Collection Interface
Individuals often get confounded between the collections system and Collection Interface.
The Collection interface is the root interface of the collections system. The structure incorporates different interfaces too: Map and Iterator. These interfaces may likewise have subinterfaces.
Subinterfaces of the Collection Interface
As mentioned earlier, the Collection interface includes subinterfaces that are implemented by Java classes.
All the methods of the Collection interface are also present in its subinterfaces.
Here are the subinterfaces of the Collection Interface:
List Interface
The List interface is an arranged assortment that allows us to include and eliminate components like an array. To learn more, visit Java List Interface
Set Interface
The Set interface allows us to store elements in different sets similar to the set in mathematics. It cannot have duplicate elements. To learn more, visit Java Set Interface
Queue Interface
The Queue interface is used when we want to store and access elements in First In, First Out manner visit, Java Queue Interface
Java Map Interface
In Java, the Map interface allows components to be stored in key/value sets. Keys are exceptional names that can be used to get to a specific component in a map. Also, each key has a single value associated with it. To learn more, visit Java Map Interface
Java Iterator Interface
In Java, the Iterator interface provides methods that can be used to access elements of collections. To learn more, visit Java Iterator Interface
Why the Collections Framework?
The Java assortments system gives different information structures and algorithms that can be used legitimately. This has two main advantages:
- We don’t need to write code to actualize these information structures and algorithms manually.
- Our code will be significantly more effective as the assortment structure is exceptionally optimized.
- Additionally, the collections framework allows us to use a particular information structure for a specific sort of information. Here are a couple of examples,
- If we want our data to be unique, then we can use the Set interface provided by the collections framework.
- To store data in key/value pairs, we can use the Map interface.
- The ArrayList class provides the functionality of resizable arrays.
Example: ArrayList Class of Collections
Before we wrap up this tutorial, let’s take an example of the ArrayList class of the collections framework.
The ArrayList class allows us to create resizable arrays. The class implements the List interface (which is a subinterface of the Collection interface).
// The Collections framework is defined in the java.util package
import java.util.ArrayList;
class Main {
public static void main(String[] args){
ArrayList<String> animals = new ArrayList<>();
// Add elements
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("ArrayList: " + animals);
}
}
Output
ArrayList: [Dog, Cat, Horse]
In the later tutorials, we will learn about the collections framework (its interfaces and classes) in detail with the help of examples.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.