SlideShare ist ein Scribd-Unternehmen logo
1 von 46
Masudul Haque
A collection — sometimes called a container —
is simply an object that groups multiple
elements into a single unit. Collections are
used to store, retrieve, manipulate, and
communicate aggregate data.






Data Structure
Interface
Implementation
Algorithm
Concurrency










Jdk 1.0: Vector, Stack, Dictionary, Hashtable,
Enumeration
Jdk 1.2: List, Set, Map, ArrayList, LinkedList,
HashSet, TreeSet, HashMap, WeakHashMap
Jdk 1.4: RandomAccess, IdentityHashMap,
LinkedHashMap
Jdk 1.5: Queue, java.util.concurrent
Jdk 1.6: Deque, NavigableSet/Map
Jdk 1.7: TransferQueue, LinkedTransferQueue








Reduces programming effort.
Increases programming speed and quality.
Allows interoperability of unrelated data.
Reduces learning effort.
Reduces effort of to design new APIs
Foster software reuse.





Indexed access
Uses offset from memory address for fast
memory access
In Java - fixed size memory block with VMlevel support




Dynamic structure made of pointers
Easy to insert and delete new elements
No indexed access
Think of an example with ArrayList:
List<String> listA = new ArrayList<>();
listA.add("B");
listA.add("C");
listA.add("D");
listA.add("C");
for (String string : listA) { //Iteration
if (string.equals("C")) {
listA.remove("C"); //ConcurrentModificationExp
}
}
System.out.println(listA);
List<String> listA = new CopyOnWriteArrayList<>();
listA.add("B");
listA.add("C");
listA.add("D");
listA.add("C");

for (String string : listA) {
if (string.equals("C")) {
listA.remove("C");
}
}
System.out.println(listA);





Thread safe. Never throws ConcurrentModificationEception.
Copy-on-write operation.
Snapshot iterator.
Data
Structure

Sorting

Iterator

Null Support?

ArrayList

Array

No

Fail-fast

Yes

LinkedLIst

Linked list

No

Fail-fast

Yes

CopyOnWrite
ArrayList

Array

No

Snapshot

Yes
add

remove

get

contain

ArrayList

O(1)

O(n)

O(1)

O(n)

LinkedLIst

O(1)

O(1)

O(n)

O(n)

CopyOnWrite
ArrayList

O(n)

O(n)

O(1)

O(n)






Fail-fast - work on live data, but become
invalid when live data is modified
Weakly consistent - work on live data, safe,
reflect updates and deletes, but not inserts
Snapshot - work on a snapshot of the live
data, fast, safe, but possibly stale








first() : E
last() : E
headSet(E toElem) : SortedSet<E>
subSet(E fromElem, E toElem) : SortedSet<E>
tailSet(E fromElem) : SortedSet<E>
comparator() : Comparator<? super E>













pollFirst() : E
pollLast() : E
subSet(E from, boolean inclusive, E to, boolean inclusive) :
NavigableSet<E>
headSet(E to, boolean inclusive) : NavigableSet<E>
tailSet(E from, boolean inclusive) : NavigableSet<E>

ceiling(E e) : E
floor(E e) : E
higher(E e) : E
lower(E e) : E
descendingSet() : NavigableSet<E>
descendingIterator() : Iterator<E>
Data Structure

Sorting

Iterator

Nulls?

HashSet

Hash table

No

Fail-fast

Yes

LinkedHash
Set

Hash table+
Linked list

Insertion order

Fail-fast

Yes

EnumSet

Bit Vector

Natural order

Weekly
Consistent

No

TreeSet

Red-black tree

Sorted

Fail-fast

Depends

CopyOnWriteA
rraySet

Array

No

Snapshot

Yes

ConcurrentSki
pListSet

Skip list

Sorted

Weekly
Consistent

No




Series of linked list
Reasonably fast search add and remove
Lock free implementation
add

contains

next

HashSet

O(1)

O(1)

O(h/n)

LinkedHash Set

O(1)

O(1)

O(1)

EnumSet

O(1)

O(1)

O(1)

TreeSet

O(log n)

O(log n)

O(log n)

CopyOnWriteArray
Set

O(n)

O(n)

O(1)

ConcurrentSkipLis
tSet

O(log n)

O(log n)

O(1)
Throws exception

Returns special value

Insert

add(e)

offer(e)

Remove

remove()

poll()

Examine

element()

peek()
Throws
exception

Special value

Blocks

Time out

Insert

add(e)

offer(e)

put(e)

offer(e,time,
unit)

Remove

remove()

poll()

take()

poll(time,
unit)

Examine

element()

peek()

Not
applicable

Not
applicable




Balanced binary tree
Root is always highest priority
Inserts and removes cause re-balancing
Head:Throws Head: Special Tail: Throws
exception
value
exception
Insert

Remove

Examine

Tail: Special
value

addFirst(e)

offerFirst(e)

addLast(e)

offerLast(e)

removeFirst()

pollFirst()

removeLast()

pollLast()

getLast()

peekLast()

Stack: push

Queue: remove
Stack:pop

getFirst()

Queue: element

Queue: poll

peekFirst()
Queue:peek
Stack:peek

Queue: add

Queue: offer
Head

Throws
exception

Insert

addFirst(e)
Stack: push

Special value

Blocks

Time out

offerFirst(e)

putFirst(e)

offerFirst(e,
time,unit)

Queue: add

Queue: offer

takeFirst()

pollFirst(e,
time,unit)

Special value

Blocks

Time out

offerLast(e)

putLast(e)

offerLast(e,
time, unit)

Remove

removeFirst()

pollFirst()

Examine

getFirst()

peekFirst()

Queue: element

Queue:peek
Stack:peek

Tail

Throws
exception

Insert

addLast(e)

Remove
Examine

Queue: remove

Stack: push

removeLast()
Queue: remove
Stack:pop

getLast()

Queue: element

Queue: poll

pollLast()
Queue: poll

peekLast()
Queue:peek
Stack:peek

Queue: put

takeLast()

Queue: offer

pollLast(e,
time, unit)
Producers wait for consumers to receive
elements. Useful for message passing. Similar
to a broader version of SynchronousQueue.
hasWaitingConsumer() : boolean
getWaitingConsumerCount() : int
transfer(E e)
tryTransfer(E e) : boolean
tryTransfer(E e, long timeout, TimeUnit unit) :
boolean
put(E key, V value) : V
putAll(Map<? extends K, ? extends V> m)
remove(Object key) : V
clear()
containsKey(Object key) : boolean
containsValue(Object value) : boolean
isEmpty() : boolean
size() : int
get(Object key) : V
entrySet() : Set<Map.Entry<K,V>>
keySet() : Set<K>
values() : Collection<V>
firstKey() : K
lastKey() : K
headMap(K to) : SortedMap<K, V>
subMap(K from, K to) : SortedMap<K, V>
tailMap(K from) : SortedMap<K, V>
comparator() : Comparator<? super K>
firstEntry() : Map.Entry<K,V>
lastEntry() : Map.Entry<K,V>
ceilingEntry(K key) : Map.Entry<K,V>
ceilingKey(K key) : K
floorEntry(K key) : Map.Entry<K,V>
floorKey(K key) : K
higherEntry(K key) : Map.Entry<K,V>
higherKey(K key) : K
lowerEntry() : Map.Entry<K,V>
lowerEntry(K key) : K
navigableKeySet() : NavigableSet<K>
pollFirstEntry() : Map.Entry<K,V>
pollLastEntry() : Map.Entry<K,V>
headMap(K to, boolean inclusive) : NavigableMap<K,V>
subMap(K from, boolean fromInc, K to, boolean toInc) : NavigableMap<K,V>
tailMap(K from, boolean inclusive) : NavigableMap<K,V>
descendingKeySet() : NavigableSet<K>
descendingMap() : NavigableMap<K,V>
putIfAbsent(K key, V value) : V
remove(Object key, Object value) : boolean
replace(K key, V value) : V
replace(K key, V oldValue, V newValue) : boolean
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Collections - Maps
Collections - Maps Collections - Maps
Collections - Maps
 
Javasession7
Javasession7Javasession7
Javasession7
 
Sasi, cassandra on full text search ride
Sasi, cassandra on full text search rideSasi, cassandra on full text search ride
Sasi, cassandra on full text search ride
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Datastax day 2016 : Cassandra data modeling basics
Datastax day 2016 : Cassandra data modeling basicsDatastax day 2016 : Cassandra data modeling basics
Datastax day 2016 : Cassandra data modeling basics
 
Collections Java e Google Collections
Collections Java e Google CollectionsCollections Java e Google Collections
Collections Java e Google Collections
 
Array list
Array listArray list
Array list
 
R training2
R training2R training2
R training2
 
Collections Framework Begineers guide 2
Collections Framework Begineers guide 2Collections Framework Begineers guide 2
Collections Framework Begineers guide 2
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01
 
Collections generic
Collections genericCollections generic
Collections generic
 
Latent Semantic Analysis of Wikipedia with Spark
Latent Semantic Analysis of Wikipedia with SparkLatent Semantic Analysis of Wikipedia with Spark
Latent Semantic Analysis of Wikipedia with Spark
 
Apache cassandra in 2016
Apache cassandra in 2016Apache cassandra in 2016
Apache cassandra in 2016
 
Java
JavaJava
Java
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
 
Perl and Elasticsearch
Perl and ElasticsearchPerl and Elasticsearch
Perl and Elasticsearch
 
Collections lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40
 

Ähnlich wie Java-7: Collections

Week_2_Lec_6-10_with_watermarking_(1).pdf
Week_2_Lec_6-10_with_watermarking_(1).pdfWeek_2_Lec_6-10_with_watermarking_(1).pdf
Week_2_Lec_6-10_with_watermarking_(1).pdf
PrabhaK22
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptx
eyemitra1
 
20130215 Reading data into R
20130215 Reading data into R20130215 Reading data into R
20130215 Reading data into R
Kazuki Yoshida
 

Ähnlich wie Java-7: Collections (20)

Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
 
Java 103
Java 103Java 103
Java 103
 
16 containers
16   containers16   containers
16 containers
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt
 
07 java collection
07 java collection07 java collection
07 java collection
 
Week_2_Lec_6-10_with_watermarking_(1).pdf
Week_2_Lec_6-10_with_watermarking_(1).pdfWeek_2_Lec_6-10_with_watermarking_(1).pdf
Week_2_Lec_6-10_with_watermarking_(1).pdf
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptx
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
Lecture 24
Lecture 24Lecture 24
Lecture 24
 
Collections forceawakens
Collections forceawakensCollections forceawakens
Collections forceawakens
 
Java collections the force awakens
Java collections  the force awakensJava collections  the force awakens
Java collections the force awakens
 
Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2Collections Framework Beginners Guide 2
Collections Framework Beginners Guide 2
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Ds (ppt).pptx
Ds (ppt).pptxDs (ppt).pptx
Ds (ppt).pptx
 
Java Collections API
Java Collections APIJava Collections API
Java Collections API
 
collections
collectionscollections
collections
 
20130215 Reading data into R
20130215 Reading data into R20130215 Reading data into R
20130215 Reading data into R
 
DSJ_Unit III_Collection framework.pdf
DSJ_Unit III_Collection framework.pdfDSJ_Unit III_Collection framework.pdf
DSJ_Unit III_Collection framework.pdf
 
Hands on Mahout!
Hands on Mahout!Hands on Mahout!
Hands on Mahout!
 

Mehr von Masudul Haque (6)

Websocket
WebsocketWebsocket
Websocket
 
Java 9 new features
Java 9 new featuresJava 9 new features
Java 9 new features
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular Expression
 
Java-7 Concurrency
Java-7 ConcurrencyJava-7 Concurrency
Java-7 Concurrency
 
Basic java
Basic javaBasic java
Basic java
 

Kürzlich hochgeladen

An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Kürzlich hochgeladen (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

Java-7: Collections