Java Collections (OCA) Cheat Sheet
by Jianmin Feng (taotao) via cheatography.com/79308/cs/19511/
What's collection Collection Methods (cont) Iterable interface
a framework/architecture(a set of classes public boolean retainAll(Colle‐ retainAll top of collection
/interface) to store and manipulation group(‐ ction<?> c) Only one method:
single unit) of objcts public int size() size() Iterator<T> return the iterator over
sorting, searching, insert, delete, iterate etc. public void clear() clear iterator() the items of type T
many interfaces: List, Set, Queue,Dequeue public boolean isEmpty() isEmpty 4 way to iterate
many classes: ArrayList,Vector, LinkedLis‐ public boolean contains(Object contains 1. iterator hasNext(), next()
t,PriorityQueue,HashSet,TreeSet etc element) 2. for loop size()
public boolean containsAll(Col‐ contai‐ 3. for each loop
Collection framework hierarchy
lection<?> c) nsAll
4.lambda list.forEach(name->na‐
iterable --> collection -->List,Queue/Dequ‐
public Iterator iterator() iterator expression me.charAt(0)='h')
e,Set/SortedSet
public Object[] toArray() toArray forEach()
list->ArrayList,LinkedList,Vector <-Sack
public <T> T[] toArray(T[] a) toArray mapAscii.forEac‐ can be used to iterate
Queue ->PriorityQueue
type h((key, value) map
Deque ->ArrayDeque,LinkedList
public boolean equals(Object equals
SortedSet->TreeSet element) List Interface
Set->HashSet,LinkedHashSet public int hashCode() hashcode Duplicable
default Stream<E> parallelStream() ArrayList random access, add/remove
Collection Methods expensive(shift),not ordered
default Stream<E> stream()
public boolean add(E e) append LinkedList sequence access,add/remove
default Spliterator<E> spliterator()
an item cheap(no shift), ordered
public boolean addAll(Collecti‐ addAll Iterator interface Vector like ArrayList,but synchroni‐
on<? extends E> c) zed,more methods
public boolean hasNext()
public boolean remove(Object remove 1 Stack extends Vector, LIFO, more
public Object next()
element) methods
public void remove()
public boolean removeAll(Co‐ removeAll boolean push(),boolean
llection<?> c) enumeration hasMoreElement(), nextEl‐ peek(),boolean push(obj)
ement(), but no remove()
default boolean removeIf(Pre‐ removeIf
dicate<? super E> filter)
By Jianmin Feng (taotao) Not published yet. Sponsored by Readable.com
cheatography.com/taotao/ Last updated 1st May, 2019. Measure your website readability!
Page 1 of 2. https://readable.com
Java Collections (OCA) Cheat Sheet
by Jianmin Feng (taotao) via cheatography.com/79308/cs/19511/
Queue interface Comparable and Comparator interfaces iterate on map
FIFO first in first out Comparator equals(), Compare() No iterator
Ordered list of item to be processed Comparable compareTo() 1 for for ( Map.Entry<String,String>
Priori‐ no null item, ordered by each e:myMap.entrySet()){}
tyQueue priority Java Map loop
Deque interface, doubled ended key value pairs not iterable for (String k:myMap.keySet()){}
queue NoSuchElementEx‐ ClassCastException for (String v:myMap.value()){}
ArrayDeque add/remove from both end, ception 2 indrect Oterator<Map.Entry<String,Stri‐
faster than ArrayList and NullPointerExce‐ UnsupportedOper‐ iterator ng>> itr=myMao.entrySet().ite‐
Stack ption ationException rator()
Object put(Object k, add 3 stand size()
Set for loop
Object v)
unordered no duplicate, at most one null void putAll(Map m) addAll 4 myMap.foreach((k,v)->...)
Hashset forEac‐
Object remove‐ remvoe
h(l‐
LinkedLis‐ maintain insertion order, (Object k)
ambdas)
tHashSet permit nulls Object get(Object k) get
5 iterator set value myMap.get(key)
SortedSet sorted ascending/decendin‐ boolean containsK‐ ContainsKey
on key
interface g/natual ordering ey(Object k)
not efficient, not practical
TreeSet ascending order, faster boolean containsV‐ containsValue
access alue(Object v)
HashMap,Treemap and Hashable
Set entrySet( ) value->set
Java Collections HashMap: unique key, dup values;allow
Set keySet( ) key->set
null values and null keys
java.util.Colle‐ Static methods
Collection values( ) value->collection
ctions TreeMap ordered object
int size( ) size
max() min() HashTable synchonized, no nulls
void clear( ) clear
sort() shuffle()
boolean isEmpty( ) isEmpty
binarySearch() copy()
boolean equals‐ equals
reverse() synchronizedCollec‐
(Object obj)
tion()
int hashCode( ) hashcode
disjoin(): split into 3 collection w/o commons
By Jianmin Feng (taotao) Not published yet. Sponsored by Readable.com
cheatography.com/taotao/ Last updated 1st May, 2019. Measure your website readability!
Page 2 of 2. https://readable.com