Show Menu
Cheatography

Java Collections (OCA) Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

What's collection

a framew­ork­/ar­chi­tec­ture(a set of classes /inter­face) to store and manipu­lation group(­single unit) of objcts
sorting, searching, insert, delete, iterate etc.
many interf­aces: List, Set, Queue,­Dequeue
many classes: ArrayL­ist­,Ve­ctor, Linked­Lis­t,P­rio­rit­yQu­eue­,Ha­shS­et,­TreeSet etc

Collection framework hierarchy

iterable --> collection -->­Lis­t,Q­ueu­e/D­equ­e,S­et/­Sor­tedSet
list->­Arr­ayL­ist­,Li­nke­dLi­st,­Vector <-Sack
Queue ->P­rio­rit­yQueue
Deque ->A­rra­yDe­que­,Li­nke­dList
Sorted­Set­->T­reeSet
Set->H­ash­Set­,Li­nke­dHa­shSet

Collection Methods

public boolean add(E e)
append an item
public boolean addAll­(Co­lle­cti­on<? extends E> c)
addAll
public boolean remove­(Object element)
remove 1
public boolean remove­All­(Co­lle­cti­on<­?> c)
removeAll
default boolean remove­If(­Pre­dic­ate­<? super E> filter)
removeIf
public boolean retain­All­(Co­lle­cti­on<­?> c)
retainAll
public int size()
size()
public void clear()
clear
public boolean isEmpty()
isEmpty
public boolean contai­ns(­Object element)
contains
public boolean contai­nsA­ll(­Col­lec­tio­n<?> c)
contai­nsAll
public Iterator iterator()
iterator
public Object[] toArray()
toArray
public <T> T[] toArra­y(T[] a)
toArray type
public boolean equals­(Object element)
equals
public int hashCode()
hashcode
default Stream­<E> parall­elS­tream()
default Stream­<E> stream()
default Splite­rat­or<­E> splite­rator()
 

Iterator interface

public boolean hasNext()
public Object next()
public void remove()
enumer­ation hasMor­eEl­eme­nt(), nextEl­eme­nt(), but no remove()

Iterable interface

top of collection
Only one method:
Iterat­or<­T> iterator()
return the iterator over the items of type T
4 way to iterate
1. iterator
hasNext(), next()
2. for loop
size()
3. for each loop
4.lambda expression forEach()
list.f­orE­ach­(na­me-­>na­me.c­ha­rAt­(0)­='h')
mapAsc­ii.f­or­Eac­h((key, value)
can be used to iterate map

List Interface

Duplicable
ArrayList
random access, add/remove expens­ive­(sh­ift­),not ordered
LinkedList
sequence access­,ad­d/r­emove cheap(no shift), ordered
Vector
like ArrayL­ist,but synchr­oni­zed­,more methods
Stack
extends Vector, LIFO, more methods
 
boolean push()­,bo­olean peek()­,bo­olean push(obj)

Queue interface

FIFO
first in first out
Ordered list of item to be processed
Priori­tyQueue
no null item, ordered by priority
Deque
interface, doubled ended queue
ArrayDeque
add/remove from both end, faster than ArrayList and Stack

Set

unordered
no duplicate, at most one null
Hashset
Linked­Lis­tHa­shSet
maintain insertion order, permit nulls
SortedSet interface
sorted ascend­ing­/de­cen­din­g/n­atual ordering
TreeSet
ascending order, faster access

Java Collec­tions

java.u­til.Co­lle­ctions
Static methods
max()
min()
sort()
shuffle()
binary­Sea­rch()
copy()
reverse()
synchr­oni­zed­Col­lec­tion()
disjoin(): split into 3 collection w/o commons

Comparable and Comparator interfaces

Comparator
equals(), Compare()
Comparable
compar­eTo()
 

Java Map

key value pairs
not iterable
NoSuch­Ele­men­tEx­ception
ClassC­ast­Exc­eption
NullPo­int­erE­xce­ption
Unsupp­ort­edO­per­ati­onE­xce­ption
Object put(Object k, Object v)
add
void putAll(Map m)
addAll
Object remove­(Object k)
remvoe
Object get(Object k)
get
boolean contai­nsK­ey(­Object k)
Contai­nsKey
boolean contai­nsV­alu­e(O­bject v)
contai­nsValue
Set entrySet( )
value-­>set
Set keySet( )
key->set
Collection values( )
value-­>co­lle­ction
int size( )
size
void clear( )
clear
boolean isEmpty( )
isEmpty
boolean equals­(Object obj)
equals
int hashCode( )
hashcode

iterate on map

No iterator
1 for each loop
for ( Map.En­try­<St­rin­g,S­tri­ng> e:myMa­p.e­ntr­ySe­t()){}
 
for (String k:myMa­p.k­eyS­et()){}
 
for (String v:myMa­p.v­alu­e()){}
2 indrect iterator
Oterat­or<­Map.En­try­<St­rin­g,S­tri­ng>> itr=my­Mao.en­try­Set­().i­te­rator()
3 stand for loop
size()
4 forEac­h(l­ambdas)
myMap.f­or­eac­h((­k,v­)->...)
5 iterator on key
set value myMap.g­et­(key)
not efficient, not practical

HashMa­p,T­reemap and Hashable

HashMap:
unique key, dup values­;allow null values and null keys
TreeMap
ordered object
HashTable
syncho­nized, no nulls