You are on page 1of 1

Java Collections Cheat Sheet

What can your collection do for you?


Notable Java collections
libraries Your data Operations on your collections

Collection class Thread-safe alternative Duplicate Order of iteration Performant Random access
Individual Key-value Primitive
element ‘contains’
elements pairs support
support FIFO Sorted LIFO check By key By value By index
Fastutil
HashMap ConcurrentHashMap
http://fastutil.di.unimi.it/
Fast & compact type-specific collections for Java Maps.synchronizedBiMap
HashBiMap (Guava)
(new HashBiMap())
Great default choice for collections of primitive
ArrayListMultimap Maps.synchronizedMultiMap
types, like int or long. Also handles big
(Guava) (new ArrayListMultimap())
collections with more than 231 elements well.
Collections.synchronizedMap
LinkedHashMap
(new LinkedHashMap())

TreeMap ConcurrentSkipListMap * *
Guava
https://github.com/google/guava Int2IntMap (Fastutil)
Google Core Libraries for Java 6+
ArrayList CopyOnWriteArrayList
Perhaps the default collection library for Java
projects. Contains a magnitude of convenient Collections.newSetFromMap
HashSet
(new ConcurrentHashMap<>())
methods for creating collection, like fluent
builders, as well as advanced collection types. IntArrayList (Fastutil)

PriorityQueue PriorityBlockingQueue **

Eclipse Collections ArrayDeque ArrayBlockingQueue ** **


https://www.eclipse.org/collections/
* O(log(n)) complexity, while all others are O(1) - constant time ** when using Queue interface methods: offer() / poll()
Features you want with the collections you need
Previously known as gs-collections, this library
includes almost any collection you might How fast are your collections?
need: primitive type collections, multimaps,
bidirectional maps and so on. Random access Search /
Collection class Insert Remember, not all operations are equally fast. Here’s a reminder
by index / key Contains
of how to treat the Big-O complexity notation:

ArrayList O(1) O(n) O(n) O(1) - constant time, really fast, doesn’t depend on the
JCTools size of your collection
https://github.com/JCTools/JCTools HashSet O(1) O(1) O(1)
Java Concurrency Tools for the JVM. O(log(n)) - pretty fast, your collection size has to be
If you work on high throughput concurrent extreme to notice a performance impact
HashMap O(1) O(1) O(1)
applications and need a way to increase your
O(n) - linear to your collection size: the larger your
performance, check out JCTools.
TreeMap O(log(n)) O(log(n)) O(log(n)) collection is, the slower your operations will be

You might also like