java-7: collections
Post on 11-Nov-2014
434 Views
Preview:
DESCRIPTION
TRANSCRIPT
Java-7: CollectionsMasudul 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.
What is collection?
Data Structure Interface Implementation Algorithm Concurrency
Collection Framework
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
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
Collection
Collection Interface
List Interface
List Implementations
Indexed access Uses offset from memory address for fast
memory access In Java - fixed size memory block with VM-
level support
ArrayList
Dynamic structure made of pointers Easy to insert and delete new elements No indexed access
LinkedList
ArrayList vs LinkedList
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
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
Thread safe. Never throws ConcurrentModifi- cationEception.
Copy-on-write operation. Snapshot iterator.
CopyOnWriteArrayList
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
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
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
Set Interface
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
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
Set Implementation
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
Series of linked list Reasonably fast search add and remove Lock free implementation
Skip Lists
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
Queues & Deques
Throws exception Returns special value
Insert add(e) offer(e)
Remove remove() poll()
Examine element() peek()
Queue Method
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
Queue Implementation
Balanced binary tree Root is always highest priority Inserts and removes cause re-balancing
Data Structure: PriorityQueue
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
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
Deque Implementation
Comparing Queue Implementation
Queue Performance
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
Map Interface
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
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
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
putIfAbsent(K key, V value) : Vremove(Object key, Object value) : booleanreplace(K key, V value) : Vreplace(K key, V oldValue, V newValue) : boolean
ConcurrentMap
Map Implementation
Comparing Map Implementation
Map Performance
Collections Class
top related