0% found this document useful (0 votes)
191 views2 pages

Java Collections Cheat Sheet

The document provides a summary of key Java collection concepts including: - Collections provide a framework for storing and manipulating groups of objects through interfaces like List, Set, Queue, and Deque and classes like ArrayList, Vector, LinkedList, PriorityQueue, and HashSet. - Common collection methods include add, remove, contains, iterator, size, and clear. - Interfaces like List, Set, Queue define behavior while classes implement the interfaces. For example, ArrayList and LinkedList both implement the List interface. - Maps store objects in key-value pairs and provide methods like put, get, remove, and size. Maps are not iterable but provide entrySet, keySet and values views.

Uploaded by

soikot rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views2 pages

Java Collections Cheat Sheet

The document provides a summary of key Java collection concepts including: - Collections provide a framework for storing and manipulating groups of objects through interfaces like List, Set, Queue, and Deque and classes like ArrayList, Vector, LinkedList, PriorityQueue, and HashSet. - Common collection methods include add, remove, contains, iterator, size, and clear. - Interfaces like List, Set, Queue define behavior while classes implement the interfaces. For example, ArrayList and LinkedList both implement the List interface. - Maps store objects in key-value pairs and provide methods like put, get, remove, and size. Maps are not iterable but provide entrySet, keySet and values views.

Uploaded by

soikot rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Collections (OCA) Cheat Sheet

by Jianmin Feng (taotao) via cheatography.com/79308/cs/19511/

What's collection Collection Methods (cont) Iterable interface

a framew​ork​/ar​chi​tec​ture(a set of classes public boolean retain​All​(Co​lle​‐ retainAll top of collection


/inter​face) to store and manipu​lation group(​‐ cti​on<​?> c) Only one method:
single unit) of objcts public int size() size() Iterat​or<​T> return the iterator over
sorting, searching, insert, delete, iterate etc. public void clear() clear iterator() the items of type T
many interf​aces: List, Set, Queue,​Dequeue public boolean isEmpty() isEmpty 4 way to iterate
many classes: ArrayL​ist​,Ve​ctor, Linked​Lis​‐ public boolean contai​ns(​Object contains 1. iterator hasNext(), next()
t,P​rio​rit​yQu​eue​,Ha​shS​et,​TreeSet etc element) 2. for loop size()
public boolean contai​nsA​ll(​Col​‐ contai​‐ 3. for each loop
Collection framework hierarchy
lec​tio​n<?> c) nsAll
4.lambda list.f​orE​ach​(na​me-​>na​‐
iterable --> collection -->​Lis​t,Q​ueu​e/D​equ​‐
public Iterator iterator() iterator expression me.c​ha​rAt​(0)​='h')
e,S​et/​Sor​tedSet
public Object[] toArray() toArray forEach()
list->​Arr​ayL​ist​,Li​nke​dLi​st,​Vector <-Sack
public <T> T[] toArra​y(T[] a) toArray mapAsc​ii.f​or​Eac​‐ can be used to iterate
Queue ->P​rio​rit​yQueue
type h((key, value) map
Deque ->A​rra​yDe​que​,Li​nke​dList
public boolean equals​(Object equals
Sorted​Set​->T​reeSet element) List Interface

Set->H​ash​Set​,Li​nke​dHa​shSet public int hashCode() hashcode Duplicable

default Stream​<E> parall​elS​tream() ArrayList random access, add/remove


Collection Methods expens​ive​(sh​ift​),not ordered
default Stream​<E> stream()
public boolean add(E e) append LinkedList sequence access​,ad​d/r​emove
default Splite​rat​or<​E> splite​rator()
an item cheap(no shift), ordered
public boolean addAll​(Co​lle​cti​‐ addAll Iterator interface Vector like ArrayL​ist,but synchr​oni​‐
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 remove​All​(Co​‐ removeAll boolean push()​,bo​olean
lle​cti​on<​?> c) enumer​ation hasMor​eEl​eme​nt(), nextEl​‐ peek()​,bo​olean push(obj)
eme​nt(), but no remove()
default boolean remove​If(​Pre​‐ removeIf
dic​ate​<? 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 compar​eTo() 1 for for ( Map.En​try​<St​rin​g,S​tri​ng>
Priori​‐ no null item, ordered by each e:myMa​p.e​ntr​ySe​t()){}
tyQueue priority Java Map loop

Deque interface, doubled ended key value pairs not iterable for (String k:myMa​p.k​eyS​et()){}
queue NoSuch​Ele​men​tEx​‐ ClassC​ast​Exc​eption for (String v:myMa​p.v​alu​e()){}
ArrayDeque add/remove from both end, ception 2 indrect Oterat​or<​Map.En​try​<St​rin​g,S​tri​‐
faster than ArrayList and NullPo​int​erE​xce​‐ Unsupp​ort​edO​per​‐ iterator ng>> itr=my​Mao.en​try​Set​().i​te​‐
Stack ption ati​onE​xce​ption 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.f​or​eac​h((​k,v​)->...)
Hashset forEac​‐
Object remove​‐ remvoe
h(l​‐
Linked​Lis​‐ maintain insertion order, (Object k)
ambdas)
tHa​shSet permit nulls Object get(Object k) get
5 iterator set value myMap.g​et​(key)
SortedSet sorted ascend​ing​/de​cen​din​‐ boolean contai​nsK​‐ Contai​nsKey
on key
interface g/n​atual ordering ey(​Object k)
not efficient, not practical
TreeSet ascending order, faster boolean contai​nsV​‐ contai​nsValue
access alu​e(O​bject v)
HashMa​p,T​reemap and Hashable
Set entrySet( ) value-​>set
Java Collec​tions HashMap: unique key, dup values​;allow
Set keySet( ) key->set
null values and null keys
java.u​til.Co​lle​‐ Static methods
Collection values( ) value-​>co​lle​ction
ctions TreeMap ordered object
int size( ) size
max() min() HashTable syncho​nized, no nulls
void clear( ) clear
sort() shuffle()
boolean isEmpty( ) isEmpty
binary​Sea​rch() copy()
boolean equals​‐ equals
reverse() synchr​oni​zed​Col​lec​‐
(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

You might also like