ZT Java Collections Cheat Sheet
ZT Java Collections Cheat Sheet
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 **
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