Masudul Haque
A collection — sometimes called a container —
is simply an object that groups multiple
elements into a single unit. Collec...






Data Structure
Interface
Implementation
Algorithm
Concurrency










Jdk 1.0: Vector, Stack, Dictionary, Hashtable,
Enumeration
Jdk 1.2: List, Set, Map, ArrayList, LinkedList,...








Reduces programming effort.
Increases programming speed and quality.
Allows interoperability of unrelated da...





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

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")...
List<String> listA = new CopyOnWriteArrayList<>();
listA.add("B");
listA.add("C");
listA.add("D");
listA.add("C");

for (S...





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-...
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...






Fail-fast - work on live data, but become
invalid when live data is modified
Weakly consistent - work on live dat...








first() : E
last() : E
headSet(E toElem) : SortedSet<E>
subSet(E fromElem, E toElem) : SortedSet<E>
tailSet(...













pollFirst() : E
pollLast() : E
subSet(E from, boolean inclusive, E to, boolean inclusive) :
Naviga...
Data Structure

Sorting

Iterator

Nulls?

HashSet

Hash table

No

Fail-fast

Yes

LinkedHash
Set

Hash table+
Linked lis...




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(...
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()...




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)...
Head

Throws
exception

Insert

addFirst(e)
Stack: push

Special value

Blocks

Time out

offerFirst(e)

putFirst(e)

offe...
Producers wait for consumers to receive
elements. Useful for message passing. Similar
to a broader version of SynchronousQ...
put(E key, V value) : V
putAll(Map<? extends K, ? extends V> m)
remove(Object key) : V
clear()
containsKey(Object key) : b...
firstKey() : K
lastKey() : K
headMap(K to) : SortedMap<K, V>
subMap(K from, K to) : SortedMap<K, V>
tailMap(K from) : Sort...
firstEntry() : Map.Entry<K,V>
lastEntry() : Map.Entry<K,V>
ceilingEntry(K key) : Map.Entry<K,V>
ceilingKey(K key) : K
floo...
putIfAbsent(K key, V value) : V
remove(Object key, Object value) : boolean
replace(K key, V value) : V
replace(K key, V ol...
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Java-7: Collections
Upcoming SlideShare
Loading in …5
×

Java-7: Collections

818 views
597 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
818
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
40
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Java-7: Collections

  1. 1. Masudul Haque
  2. 2. 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.
  3. 3.      Data Structure Interface Implementation Algorithm Concurrency
  4. 4.       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
  5. 5.       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.
  6. 6.    Indexed access Uses offset from memory address for fast memory access In Java - fixed size memory block with VMlevel support
  7. 7.    Dynamic structure made of pointers Easy to insert and delete new elements No indexed access
  8. 8. 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);
  9. 9. 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);
  10. 10.    Thread safe. Never throws ConcurrentModificationEception. Copy-on-write operation. Snapshot iterator.
  11. 11. 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
  12. 12. 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)
  13. 13.    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
  14. 14.       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>
  15. 15.            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>
  16. 16. 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
  17. 17.    Series of linked list Reasonably fast search add and remove Lock free implementation
  18. 18. 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)
  19. 19. Throws exception Returns special value Insert add(e) offer(e) Remove remove() poll() Examine element() peek()
  20. 20. 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
  21. 21.    Balanced binary tree Root is always highest priority Inserts and removes cause re-balancing
  22. 22. 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
  23. 23. 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)
  24. 24. 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
  25. 25. 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>
  26. 26. 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>
  27. 27. 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>
  28. 28. 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

×