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:
- lists: a series of objects held in a specific order, which may include duplicates;
- sets: define a collection in which a given item is "present or not": in other words,
they do not allow duplicates and the items in them do not necessarily have any specific ordering;
- queues: these are like lists, but have items 'added to one end and removed from the other', possibly
imposing some kind of priority as they are added;
- maps: associate a series of 'keys' with 'values', for purposes such as lookup tables
(in other programming languages, these are sometimes referred to as dictionaries);
- trees: organise objects into a structure that allows efficient searches within
ranges of values.
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:
- if you just need a collection of "standard" objects (boring old Strings,
Numbers etc provided by the Java class libraries),
then you can take a fairly practical approach and start using
lists and the standard collections classes without worrying too much about how they work– we'll take a practical look at
lists, sets and maps
and use cases such as sorting a list of strings;
- if you're more or less familiar with what collections are, but are having trouble deciding
which class to use, see the section on How to choose
which Java collection class to use;
- if you need to store instances of your own class in some types of collection,
then you will generally need to understand a little bit (but not all
the details) about how they work, in particular how HashMaps work
via the technique of hashing– however,
you may wish to consult this site's hash function guidelines
if you don't want to get too bogged down in the theory;
- in some cases, you may need to have a fuller understanding about how collection classes
work in order to make decisions relating to performance.
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.
If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.
Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.