English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Java Other Topics

Java Collections Framework

In this tutorial, we will learn about different interfaces of the Java collection framework.

Java CollectionThe framework provides a set of interfaces and classes to implement various data structures and algorithms.

For example, the LinkedList class of the collection framework provides an implementation of the doubly linked list data structure.

Interfaces of the collection framework

The Java collection framework provides various interfaces. These interfaces include several methods for performing different operations on collections.

We will learn about these interfaces, their sub-interfaces, and the implementations in various classes in the following chapters. Let's briefly understand the commonly used interfaces in this tutorial.

Java Collection (Collection) Interface

The Collection interface is the root interface of the collection framework hierarchy.

Java does not provide a direct implementation of the Collection interface, but provides implementations for its subinterfaces List, Set, and Queue. For more information, please visit:Java Collection Interfaces

Collection Framework vs Collection Interface

People often feel confused between the collection framework and the Collection interface.

The Collection interface is the root interface of the collection framework. The framework also includes other interfaces: Map and Iterator. These interfaces may also have subinterfaces.

Subinterfaces of the Collection Interface

As mentioned earlier, the Collection interface includes subinterfaces implemented by Java classes.

All methods of the Collection interface also exist in its subinterfaces.

The following three interfaces are subinterfaces of the Collection interface:

1, List Interface

The List interface is an ordered collection that allows us to add and delete elements like an array. For more information, please visitJava List Interface

2, Set Interface

The Set interface allows us to store elements in different collections, similar to sets in mathematics. It cannot have duplicate elements. For more information, please visitJava Set Interface

3, Queue Interface

When we want toFirst in first outWhen accessing elements in a first-in-first-out manner, you can use the Queue interface. For more information, please visitJava Queue Interface

Java Map Interface

In Java, the Map interface allows elements to be stored and accessed in the form ofKey/ValueData is stored in the form of key-value pairs. The key is a unique name that can be used to access specific elements in the map. Moreover, each key is associated with a value. For more information, please visitJava Map Interface

Java Iterator Interface

In Java, the Iterator interface provides methods for accessing collection elements. For more information, please visitJava Iterator Interface

Why use the collection framework?

The Java collection framework provides various data structures and algorithms that can be used directly. This has two main advantages:

  • We do not need to manually write code to implement these data structures and algorithms.

  • With the high optimization of the collection framework, our code will be more efficient.

In addition, the collection framework allows us to use specific data structures for specific types of data. Here are some examples:

  • If we want our data to be unique, we can use the Set interface provided by the collection framework.

  • To store data with a key/Data is stored in the form of key-value pairs, and the Map interface can be used.

  • The ArrayList class provides the functionality of an adjustable-size array.

Example: ArrayList collection class

Before ending this tutorial, let us take a look at the collections framework'sArrayList classfor example.

This ArrayList class allows us to create an adjustable-size array. This class implements the List interface (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 Element
        animals.add("Dog");
        animals.add("Cat");
        animals.add("Horse");
        System.out.println("ArrayList: " + animals);
    }
}

Output:

ArrayList: [Dog, Cat, Horse]

In the following tutorials, we will get a detailed understanding of the collections framework (its interfaces and classes) through examples.