Java-7: Collections

46
Java-7: Collections Masudul Haque

description

 

Transcript of Java-7: Collections

Page 1: Java-7: Collections

Java-7: CollectionsMasudul Haque

Page 2: Java-7: Collections

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.

What is collection?

Page 3: Java-7: Collections

Data Structure Interface Implementation Algorithm Concurrency

Collection Framework

Page 4: Java-7: Collections

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

History

Page 5: Java-7: Collections

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.

Collection Benefits

Page 6: Java-7: Collections

Collection

Page 7: Java-7: Collections
Page 8: Java-7: Collections

Collection Interface

Page 9: Java-7: Collections

List Interface

Page 10: Java-7: Collections

List Implementations

Page 11: Java-7: Collections

Indexed access Uses offset from memory address for fast

memory access In Java - fixed size memory block with VM-

level support

ArrayList

Page 12: Java-7: Collections

Dynamic structure made of pointers Easy to insert and delete new elements No indexed access

LinkedList

Page 13: Java-7: Collections

ArrayList vs LinkedList

Page 14: Java-7: Collections

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);

CopyOnWriteArrayList

Page 15: Java-7: Collections

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);

CopyOnWriteArrayList

Page 16: Java-7: Collections

Thread safe. Never throws ConcurrentModifi- cationEception.

Copy-on-write operation. Snapshot iterator.

CopyOnWriteArrayList

Page 17: Java-7: Collections

Data Structure Sorting Iterator Null

Support?

ArrayList Array No Fail-fast Yes

LinkedLIst Linked list No Fail-fast Yes

CopyOnWriteArrayList Array No Snapshot Yes

List Comparison

Page 18: Java-7: Collections

add remove get contain

ArrayList O(1) O(n) O(1) O(n)

LinkedLIst O(1) O(1) O(n) O(n)

CopyOnWriteArrayList O(n) O(n) O(1) O(n)

List Performance Comparison

Page 19: Java-7: Collections

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

Iterator

Page 20: Java-7: Collections

Set Interface

Page 21: Java-7: Collections

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>

Sorted Set

Page 22: Java-7: Collections

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>

NavigableSet

Page 23: Java-7: Collections

Set Implementation

Page 24: Java-7: Collections

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

CopyOnWriteArraySet Array No Snapshot Yes

ConcurrentSkipListSet Skip list Sorted Weekly

Consistent No

Comparing Set

Page 25: Java-7: Collections

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

Skip Lists

Page 26: Java-7: Collections

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)

CopyOnWriteArraySet O(n) O(n) O(1)

ConcurrentSkipListSet O(log n) O(log n) O(1)

Comparing Set Performance

Page 27: Java-7: Collections

Queues & Deques

Page 28: Java-7: Collections

Throws exception Returns special value

Insert add(e) offer(e)

Remove remove() poll()

Examine element() peek()

Queue Method

Page 29: Java-7: Collections

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

BlockingQueue Method

Page 30: Java-7: Collections

Queue Implementation

Page 31: Java-7: Collections

Balanced binary tree Root is always highest priority Inserts and removes cause re-balancing

Data Structure: PriorityQueue

Page 32: Java-7: Collections

Head:Throws exception

Head: Special value

Tail: Throws exception

Tail: Special value

Insert addFirst(e)Stack: push

offerFirst(e) addLast(e)Queue: add

offerLast(e) Queue: offer

RemoveremoveFirst()Queue: remove

Stack:pop

pollFirst() Queue: poll

removeLast() pollLast()

Examine getFirst() Queue: element

peekFirst()Queue:peek Stack:peek

getLast() peekLast()

Deque Method

Page 33: Java-7: Collections

Head Throws exception Special value Blocks Time out

Insert addFirst(e)Stack: push

offerFirst(e) putFirst(e)Queue: add

offerFirst(e, time,unit) Queue: offer

Remove removeFirst()Queue: remove

pollFirst() Queue: poll

takeFirst() pollFirst(e, time,unit)

Examine getFirst() Queue: element

peekFirst()Queue:peek Stack:peek

BlockingDeque Method

Tail Throws exception Special value Blocks Time out

Insert addLast(e)Stack: push

offerLast(e) putLast(e)Queue: put

offerLast(e, time, unit) Queue: offer

RemoveremoveLast()Queue: remove

Stack:pop

pollLast() Queue: poll

takeLast() pollLast(e, time, unit)

Examine getLast() Queue: element

peekLast()Queue:peek Stack:peek

Page 34: Java-7: Collections

Deque Implementation

Page 35: Java-7: Collections

Comparing Queue Implementation

Page 36: Java-7: Collections

Queue Performance

Page 37: Java-7: Collections

Producers wait for consumers to receive elements. Useful for message passing. Similar to a broader version of SynchronousQueue.

hasWaitingConsumer() : booleangetWaitingConsumerCount() : inttransfer(E e)tryTransfer(E e) : booleantryTransfer(E e, long timeout, TimeUnit unit) : boolean

TranserQueue

Page 38: Java-7: Collections

Map Interface

Page 39: Java-7: Collections

put(E key, V value) : VputAll(Map<? extends K, ? extends V> m)remove(Object key) : Vclear()containsKey(Object key) : booleancontainsValue(Object value) : booleanisEmpty() : booleansize() : intget(Object key) : VentrySet() : Set<Map.Entry<K,V>>keySet() : Set<K>values() : Collection<V>

Map Interface

Page 40: Java-7: Collections

firstKey() : KlastKey() : KheadMap(K to) : SortedMap<K, V>subMap(K from, K to) : SortedMap<K, V>tailMap(K from) : SortedMap<K, V>comparator() : Comparator<? super K>

Sorted Map

Page 41: Java-7: Collections

firstEntry() : Map.Entry<K,V>lastEntry() : Map.Entry<K,V>ceilingEntry(K key) : Map.Entry<K,V>ceilingKey(K key) : KfloorEntry(K key) : Map.Entry<K,V>floorKey(K key) : KhigherEntry(K key) : Map.Entry<K,V>higherKey(K key) : KlowerEntry() : Map.Entry<K,V>lowerEntry(K key) : KnavigableKeySet() : 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>

NavigableMap

Page 42: Java-7: Collections

putIfAbsent(K key, V value) : Vremove(Object key, Object value) : booleanreplace(K key, V value) : Vreplace(K key, V oldValue, V newValue) : boolean

ConcurrentMap

Page 43: Java-7: Collections

Map Implementation

Page 44: Java-7: Collections

Comparing Map Implementation

Page 45: Java-7: Collections

Map Performance

Page 46: Java-7: Collections

Collections Class