Tech and Media Labs
This site uses cookies to improve the user experience.

Java Collections Tutorial

Jakob Jenkov
Last update: 2019-07-07

The Java Collections API provide Java developers with a set of classes and interfaces that makes it easier to work with collections of objects, e.g. lists, maps, stacks etc.

Rather than having to write your own collection classes, Java provides these ready-to-use collection classes for you. This tutorial will look closer at the Java Collections, as they are also sometimes referred to, and more specifically the Java Collections available in Java 8 and later.

The purpose of this tutorial is to give you an overview of the Java Collection classes. Thus it will not describe each and every little detail of the Java Collection classes. But, once you have an overview of what is there, it is much easier to read the rest in the JavaDoc afterwards.

Java Collection Core Interfaces

The core interfaces of the Java Collection API are:

Most of these core interfaces are explained in more detail in their own tutorials, but I will give you a quick introduction in the following sections.

The Java Collections API also contains useful implementations of the above interfaces, and a few other utility classes. Some of these are:

Java Collection

The Java Collection interface represents the operations possible on a generic collection, like on a List, Set, Stack, Queue and Deque. For instance, methods to access the elements based on their index are available in the Java Collection interface. The Java Collection interface is explained in more detail in the Java Collection interface tutorial.

Java List

The Java List interface represents an ordered collection of objects. By ordered means, that you can access the elements in the order they occur in the list. The List interface is explained in more detail in the Java List Interface tutorial.

Java Set

The Java Set interface represents an unordered collection of objects. Unlike the List, a Set does not allow you to access the elements of a Set in any guaranteed order. There are Set implementations that order elements based on their natural ordering, but the Set interface itself provides no such guarantees. The Set interface is explained in more detail in the Java Set tutorial.

Java Map

The Java Map interface represents a mapping between sets of keys and values. Both keys and values are objects. You insert a key + value pair into a Map, and later you can retrieve the value via the key - meaning you only need the key to read the value out of the Map again later. The Map interface is explained in more detail in the Java Map tutorial.

Java Stack

The Java Stack interface represents a classical stack data structure, where elements can be pushed to the top of the stack, and popped off from the top of the stack again later. The Stack interface is explained in more detail in the Java Stack tutorial.

Java Queue

The Java Queue interface represents a classical queue data structure, where objects are inserted into one end of the queue, and taken off the queue in the other end of the queue. This is the opposite of how you use a stack. The Queue interface is explained in more detail in the Java Queue tutorial.

Java Deque

The Java Deque interface represents a double ended queue, meaning a data structure where you can insert and remove elements from both ends of the queue. I guess you could have called it a double ended stack too. The deque interface is explained in more detail in the Java Deque tutorial.

Java Iterator

The Java Iterator interface represents a component that is capable of iterating a Java collection of some kind. For instance, a List or a Set. You can obtain an Iterator instance from the Java Set, List, Map etc.

Java Iterable

The Java Iterable interface is very similar in responsibility to the Java Iterator interface. The Iterable interface allows a Java Collection to be iterated using the for-each loop in Java. The Java Iterable interface is explained in more detail in my tutorial about the Java Iterable interface tutorial. The Java Iterable interface is not actually a part of the Java Collection API, but it is used so often with the Java Collection API that I have included the tutorial as part of the Java Collection tutorial. The for-each loop is explained in my Java for Loop tutorial.

Java Collection Packages

Most of the Java collections are located in the java.util package. Java also has a set of concurrent collections in the java.util.concurrent package. This tutorial will not describe the concurrent collections. These are described in my Java util concurrent tutorial.

Overview of Java Collections

To help you get an overview of the Java Collections classes and interfaces, the first text in this Java Collections tutorial is the Java Collections Overview text.

Java Collections and Generics

The fifth text in this Java Collections tutorial covers how to use Generics in Java Collections. Generics is very useful when working with Java's Collection classes. You can find the tutorial at Java Generic Collections.

Java Generics in general is explained in more detail in my Java Generics tutorial.

Java Collections and the equals() and hashCode() Methods

Many of the core components in the Java Collection API rely on the correct implementation of the hashCode() and equals() methods. The Java hashCode and equals methods tutorial explain how these methods work, and why they are central to the core Java collection components.

Jakob Jenkov

Copyright  Jenkov Aps
Close TOC