0% found this document useful (0 votes)
42 views5 pages

Basic Collection Types: Collection Type Functionality Typical Uses

The document discusses different collection types in Java and provides guidance on which to use for different situations. It covers Lists, Sets, Maps, and Queues. For Lists, it recommends ArrayList for most cases, LinkedList when efficient random access is not needed, and CopyOnWriteArrayList for concurrent reads. For Sets and Maps, it recommends different implementations based on whether ordered keys, concurrent access, or fixed ordering is required.

Uploaded by

isaac_b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views5 pages

Basic Collection Types: Collection Type Functionality Typical Uses

The document discusses different collection types in Java and provides guidance on which to use for different situations. It covers Lists, Sets, Maps, and Queues. For Lists, it recommends ArrayList for most cases, LinkedList when efficient random access is not needed, and CopyOnWriteArrayList for concurrent reads. For Sets and Maps, it recommends different implementations based on whether ordered keys, concurrent access, or fixed ordering is required.

Uploaded by

isaac_b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Basic collection types


The first part of the decision is choosing what "basic category" of organisation
or functionality your data needs to have. The broad types are as follows:

Collection
type

Functionality

List

Essentially a variablesize array;

You can usually


add/remove items at
any arbitrary position;

The order of the items


is well defined (i.e. you
can say what position a
given item goes in in
the list).

Things can be "there or


not" when you add
items to a set, there's
no notion of how many
times the item was
added, and usually no
notion of ordering.

Typical uses
Most cases where you just need to
store or iterate through a "bunch of
things" and later iterate through them.

Set

Map

Stores
an association or
mapping between
"keys" and "values"

Remembering "which items


you've already processed", e.g.
when doing a web crawl;

Making other yes-no


decisions about an item, e.g. "is
the item a word of English", "is
the item in the database?" , "is
the item in this category?" etc.

Used in cases where you need to say


"for a given X, what is the Y"? It is
often useful for implementing inmemory caches or indexes. For
example:

For a given user ID, what is their


cached name/User object?

For a given IP address, what is


the cached country code?

For a given string, how many


instances have I seen?

Often used in managing


tasks performed by different
threads in an application (e.g.
one thread receives incomming
connections and puts them on a
queue; other "worker" threads
take connections off the queue
for processing);

For traversing hierarchical


structures such as a filing
system, or in general where you
need to remember "what data
to process next", whilst also
adding to that list of data;

Related to the previous point,


queues crop up in various
algorithms, e.g. build the
encoding tree for Huffman
compression.

Queue

Like a list, but where


you only ever access
theends of the list
(typically, you add to
one end and remove
from the other).

Which Java List to use?

A list is the simplest of structures. It keeps its elements in the same order in
which they are inserted and allows duplicates. There are essentially three
underlying list classes:
Class

ArrayList

Features/implementation

When to use

Allows elements to be
efficiently read by index.

Adding/removing the last In most cases.


element is efficient.

Not synchronized in any


way.

LinkedList

First and last elements


can be accessed
efficiently;

Other
elements cannot be
efficiently accessed by
index;

Not synchronized in any


way.

Allows safe concurrent


access;

Reads are efficient and


non-blocking;

Modifications are not


efficient (since a brand
new copy of the list is
taken each time).

CopyOnWriteArrayList

Effectively, functions as a
non-synchronized queue. In
practice, rarely used: when
you need a queue, you often
need it to be concurrent or to
provide other functionality;
other implementations are
often more useful.

Where you need concurrent


access and where frequency
of reads far outweights
frequency of modifications.

If you need concurrent access to a list and CopyOnWriteArrayList is not


appropriate (either because the list is large or because reads don't outnumber
writes), then the best you can really do is place a synchronized wrapper around
an ordinary ArrayList:
List l = Collections.synchronizedList(new ArrayList());

Note that this gives you thread-safe access, but it's not truly concurrent in the
sense that each access to the list will lock the entire list during the access.
Remember if you do this that you must always synchronize on the list
while iterating over it (and in some cases this could be bad for concurrency).
In practice, you should think carefully whether this type of list makes much
sense. If a list is being continually altered by different threads, for example,
what value do the individual index numbers really have?
Which Java Map to use?

The JDK provides various Map implementations depending on:

whether you need to maintain the keys in sorted order, in some fixed
order or whether no particular order is required;

whether you require efficient concurrent access (i.e. where multiple


threads can access the map efficiently and can perform atomic
updateson the map).

Depending on these requirements, the various Map implementations are as


follows:
Ordering of keys

Non-concurrent

Concurrent

No particular order

HashMap

ConcurrentHashMap

Sorted

TreeMap

ConcurrentSkipListMap

Fixed

LinkedHashMap

Which Java Set to use?

Conceptually, a set serves to record whether or not an object belongs to a


particular group. But a set is usually implemented as a degenerate type of map,
in which each item added to the set is mapped to some special object meaning
"present in the set". Therefore, especially in the non-current case, the choices
in deciding which set implementation to use are largely similar to in the
previous section: do you need a predictable iteration order and/or concurrent
access? The available Set implementations are then as follows:
Ordering of keys

Non-concurrent

Concurrent

No particular order

HashSet

Sorted

TreeSet

ConcurrentSkipListSet

Fixed

LinkedHashSet

CopyOnWriteArraySet

Perhaps surprisingly, Java doesn't provide a set implementation based


on ConcurrentHashMap, though you could make such a subclass trivially yourself.
Another option that requires no code is to use a ConcurrentSkipListSet, although
you will be paying (in efficiency) for sorting that you don't really need1.
As with CopyOnWriteArrayList (on which it is actually based),
the CopyOnWriteArraySet class is suited to cases where the set is relatively small

and reads far outweight writes. Any modification of the set is expensive (since a
brand new copy is created each time), but reads are non-blocking.

1. The time taken to access a skip list structure increases by some constant amount each
time the number of elements doubles; the time to access a hash table, as
in HashMap and HashSet, is effectively constant no matter how many elements are in the
set).

Which Java Queue to use?

See the separate section on Java queues for a list of the various queue classes
and their functionality.
As mentioned above, if you need a queue that provides no extra features such
as thread-safe access or sorting, then a simple LinkedList can be used.

You might also like