Docsity

Java Collections Cheat Sheet, Cheat Sheet of Java Programming

Toggle sidebar
bg1
How fast are your collections?
Collection class Random access
by index / key
Search /
Contains Insert Remember, not all operations are equally fast. Here’s a reminder
of how to treat the Big-O complexity notation:
O(1) - constant time, really fast, doesn’t depend on the
size of your collection
O(log(n)) - pretty fast, your collection size has to be
extreme to notice a performance impact
O(n) - linear to your collection size: the larger your
collection is, the slower your operations will be
ArrayList O(1) O(n) O(n)
HashSet O(1) O(1) O(1)
HashMap O(1) O(1) O(1)
TreeMap O(log(n)) O(log(n)) O(log(n))
Java Collections Cheat Sheet
Notable Java collections
libraries
Fastutil
http://fastutil.di.unimi.it/
Fast & compact type-specific collections for Java
Great default choice for collections of primitive
types, like int or long. Also handles big
collections with more than 231 elements well.
Guava
https://github.com/google/guava
Google Core Libraries for Java 6+
Perhaps the default collection library for Java
projects. Contains a magnitude of convenient
methods for creating collection, like fluent
builders, as well as advanced collection types.
Eclipse Collections
https://www.eclipse.org/collections/
Features you want with the collections you need
Previously known as gs-collections, this library
includes almost any collection you might
need: primitive type collections, multimaps,
bidirectional maps and so on.
JCTools
https://github.com/JCTools/JCTools
Java Concurrency Tools for the JVM.
If you work on high throughput concurrent
applications and need a way to increase your
performance, check out JCTools.
What can your collection do for you?
Collection class Thread-safe alternative
Your data Operations on your collections
Individual
elements
Key-value
pairs
Duplicate
element
support
Primitive
support
Order of iteration Performant
‘contains’
check
Random access
FIFO Sorted LIFO By key By value By index
HashMap ConcurrentHashMap
HashBiMap (Guava) Maps.synchronizedBiMap
(new HashBiMap())
ArrayListMultimap
(Guava)
Maps.synchronizedMultiMap
(new ArrayListMultimap())
LinkedHashMap Collections.synchronizedMap
(new LinkedHashMap())
TreeMap ConcurrentSkipListMap
Int2IntMap (Fastutil)
ArrayList CopyOnWriteArrayList
HashSet Collections.newSetFromMap
(new ConcurrentHashMap<>())
IntArrayList (Fastutil)
PriorityQueue PriorityBlockingQueue
ArrayDeque ArrayBlockingQueue
* O(log(n)) complexity, while all others are O(1) - constant time ** when using Queue interface methods: offer() / poll()
*
**
*
**
**

Partial preview of the text

Download Java Collections Cheat Sheet and more Cheat Sheet Java Programming in PDF only on Docsity!

How fast are your collections?

Collection class Random access by index / key Contains^ Search / Insert Remember, not all operations are equally fast. Here’s a^ reminder of how to treat the Big-O complexity notation: O(1) - constant time, really fast, doesn’t depend on the size of your collection O(log(n)) - pretty fast, your collection size has to be extreme to notice a performance impact O(n) - linear to your collection size: the larger your collection is, the slower your operations will be ArrayList O(1) O(n) O(n) HashSet O(1) O(1) O(1) HashMap O(1) O(1) O(1)

TreeMap O(log(n)) O(log(n)) O(log(n))

Java Collections Cheat Sheet

Notable Java collections

libraries

Fastutil http://fastutil.di.unimi.it/ Fast & compact type-specific collections for Java Great default choice for collections of primitive types, like int or long. Also handles big collections with more than 2^31 elements well. Guava https://github.com/google/guava Google Core Libraries for Java 6+ Perhaps the default collection library for Java projects. Contains a magnitude of convenient methods for creating collection, like fluent builders, as well as advanced collection types. Eclipse Collections https://www.eclipse.org/collections/ Features you want with the collections you need Previously known as gs-collections, this library includes almost any collection you might need: primitive type collections, multimaps, bidirectional maps and so on. JCTools https://github.com/JCTools/JCTools Java Concurrency Tools for the JVM. If you work on high throughput concurrent applications and need a way to increase your performance, check out JCTools.

What can your collection do for you?

Collection class Thread-safe alternative Your data Operations on your collections Individual elements Key-value pairs Duplicate element support Primitive support Order of iteration Performant ‘contains’ check Random access FIFO Sorted LIFO By key By value By index HashMap ConcurrentHashMap HashBiMap (Guava) Maps.synchronizedBiMap (new HashBiMap()) ArrayListMultimap (Guava) Maps.synchronizedMultiMap (new ArrayListMultimap()) LinkedHashMap Collections.synchronizedMap (new LinkedHashMap()) TreeMap ConcurrentSkipListMap Int2IntMap (Fastutil) ArrayList CopyOnWriteArrayList HashSet (^) (new ConcurrentHashMap<>())Collections.newSetFromMap IntArrayList (Fastutil) PriorityQueue PriorityBlockingQueue ArrayDeque ArrayBlockingQueue * O(log(n)) complexity, while all others are O(1) - constant time ** when using Queue interface methods: offer() / poll()