Collections in Java

A collection is a general term that refers to multiple objects held together in memory in a structured way. Java comes with quite a rich Collections Framework out of the box, inlcuding a large number of classes that can be used to organise objects into lists, queues, lookup tables etc, and perform actions on these such as adding and removing items and searching and sorting them.

For pretty much any program that involves manipulating data in memory, knowing your way around the various Collections classes can be extremely important. Choosing the appropriate collection and knowing which methods to call can make many data manipulation tasks both efficient to write and efficient to run.

We mentioned informally some of the types of structures making up the Collections framework. But we will see that many of the labels that we use informally, such as "list", have quite specific definitions when they refer to a data structure. We will explore these structures in more detail over the following pages. By way of introduction, some of the key collection types include:

The collections framework provides various classes that implement these structures. It also provides utility methods to perform functions such as sorting a list of data.

Collections and streams

The collections classes are integrated with a rich Stream API that allows data to be manipulated easily in Java, often in a few lines of code.

Thread-safety and concurrency

On today's multithreaded architectures, a key feature of the Java Collections Framework are their various thread-safe implementations.

How to start learning about and using Java collections

Collections are a major topic of Java programming and getting to grips with them will inevitably be a gradual process. Here are some suggestions for getting started with the Java Collections Framework:

Advanced use of hashing techniques

More advanced readers may wish to take a look at the section on advanced use of hash codes. In this section, we see how to exploit some statistical properties of hash codes to reduce the amount of memory needed to perform certain tasks, notably duplicate elimination.