0% found this document useful (0 votes)
462 views

ZT Java Collections Cheat Sheet

This document provides a cheat sheet for Java collections, listing notable collections libraries, common collection classes, and their properties such as whether they allow duplicates, maintain insertion order, provide random access, and the time complexity of common operations. It highlights the Collection interface and classes like ArrayList, HashSet, HashMap, TreeMap, and their thread-safe alternatives. It also lists third party libraries like Guava, Eclipse Collections, Fastutil, and JCTools that provide additional collection functionality and optimizations.

Uploaded by

mpvinaykumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
462 views

ZT Java Collections Cheat Sheet

This document provides a cheat sheet for Java collections, listing notable collections libraries, common collection classes, and their properties such as whether they allow duplicates, maintain insertion order, provide random access, and the time complexity of common operations. It highlights the Collection interface and classes like ArrayList, HashSet, HashMap, TreeMap, and their thread-safe alternatives. It also lists third party libraries like Guava, Eclipse Collections, Fastutil, and JCTools that provide additional collection functionality and optimizations.

Uploaded by

mpvinaykumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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