Java Docs
Java Documentation
Java Collections Framework
The Java collections framework provides interfaces and classes.
It is used to implement various data structures and algorithms.
Example: The LinkedList class provides the implementation of the doubly-linked list data structure.
Interfaces of Collections Framework
The collections framework provides various interfaces.
These interfaces include methods to perform different operations on collections.
We will learn about these interfaces, their subinterfaces, and implementation in detail later.
Java Collection Interface
The Collection interface is the root interface of the collections framework hierarchy.
Java provides implementations for its subinterfaces like List, Set, and Queue.
Collections Framework Vs. Collection Interface
People often confuse the collections framework with the Collection Interface.
The Collection interface is only the root interface of the framework.
The framework also includes other interfaces: Map and Iterator.
Subinterfaces of the Collection Interface
The Collection interface includes subinterfaces implemented by Java classes.
All methods of the Collection interface are also present in its subinterfaces.
Here are the subinterfaces:
List Interface
The List interface is an ordered collection.
It allows adding and removing elements like an array.
Set Interface
The Set interface stores elements similar to a set in mathematics.
It cannot have duplicate elements.
Queue Interface
The Queue interface stores and accesses elements in First In, First Out (FIFO) manner.
Java Map Interface
The Map interface stores elements in key/value pairs.
Keys are unique names used to access a particular element.
Each key has a single value associated with it.
Java Iterator Interface
The Iterator interface provides methods to access elements of collections.
Why the Collections Framework?
The collections framework provides ready-to-use data structures and algorithms.
This offers two main advantages:
- We don't need to manually write code for these structures and algorithms.
- Our code is more efficient as the framework is highly optimized.
The framework allows us to use a specific data structure for a particular type of data. Examples:
- Use the
Setinterface for unique data. - Use the
Mapinterface to store data in key/value pairs. - The
ArrayListclass provides resizable array functionality.
Example: ArrayList Class of Collections
Let's look at an example of the ArrayList class.
The ArrayList class allows us to create resizable arrays.
It 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 elements
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("ArrayList: " + animals);
}
}Output:
ArrayList: [Dog, Cat, Horse]In later tutorials, we will learn about the collections framework (its interfaces and classes) in detail with the help of examples.