Collections OOP Tirgul 7 Topics Data structures Axes

  • Slides: 22
Download presentation
Collections OOP Tirgul 7

Collections OOP Tirgul 7

Topics • • • Data structures Axes of comparison Interfaces Implementations Algorithms

Topics • • • Data structures Axes of comparison Interfaces Implementations Algorithms

History • 1. 0 Vector, Dictionary, Hashtable, Stack, Enumeration • 1. 2 Collection, Iterator,

History • 1. 0 Vector, Dictionary, Hashtable, Stack, Enumeration • 1. 2 Collection, Iterator, List, Set, Map, Sorted*, Array. List, Hash. Set, Tree. Set, Hash. Map, Weak. Hash. Map • 1. 4 Random. Access, Identity. Hash. Map, Linked. Hash. Set • 1. 5 Queue, Iterable, java. util. concurrent. * • 1. 6 Deque, Navigable. Set/Map, more concurrent collections

Interfaces hierarchy Iterable Map Collection Set Sorted. Set Navigable. Set List Queue Sorted. Map

Interfaces hierarchy Iterable Map Collection Set Sorted. Set Navigable. Set List Queue Sorted. Map Deque Navigable. Map

Choosing Implementation • • • Operation Performance Data structures Sorting and ordering Duplicate elements

Choosing Implementation • • • Operation Performance Data structures Sorting and ordering Duplicate elements Null support Concurrency, Iterator style

Collection<E> interface • boolean add(E e) • boolean add. All(Collection<? Extends E> c) •

Collection<E> interface • boolean add(E e) • boolean add. All(Collection<? Extends E> c) • boolean contains(Object o) – boolean contains. All(Collection<? > c) • boolean is. Empty() • int size() • boolean remove(E e) – boolean remove/retain All(Collection<? > c) • Object[] to. Array() – E[] to. Array(E[] a) • Iterator<E> iterator()

Lists: Array. List and Linked. List Array. List and Vector: Linked. List: • Indexed

Lists: Array. List and Linked. List Array. List and Vector: Linked. List: • Indexed access • Uses offset from memory address for fast access • Fixed size memory block • Made of pointers • Easy to insert and delete • No indexed access Vector Array. List Linked. List DAST array linked list SORTED no no no NULLS no yes

Lists Performance Compared Array. List Linked. List ADD O(1) REMOVE O(n) O(1) GET O(1)

Lists Performance Compared Array. List Linked. List ADD O(1) REMOVE O(n) O(1) GET O(1) O(n) CONTAINS O(n)

Set<E> Interfaces Set Same methods as Collection Sorted. Set Navigable. Set • E first()

Set<E> Interfaces Set Same methods as Collection Sorted. Set Navigable. Set • E first() • E last() • Sorted. Set<E> head. Set(E to. Elem) • Sorted. Set<E> sub. Set(E from. Elem, E to. Elem) • Sorted. Set<E> tail. Set(E from. Elem) • Comparator<? Super E> comparator() • E poll. First() • E poll. Last() • Navigable. Set<E> sub. Set(E from. Elem, E to. Elem, boolean inclusive) • head. Set… • tail. Set… • E ceiling(E e) • E floor(E e) • E higher(E e) • E lower(E e) • descending…

Set Implementations Set Hash. Set Linked. Hash. Set Enum. Set Sorted. Set Navigable. Set

Set Implementations Set Hash. Set Linked. Hash. Set Enum. Set Sorted. Set Navigable. Set Tree. Set

Comparing Sets Hash. Set Linked. Hash. Set Enum. Set Tree. Set DAST SORTED NULLS

Comparing Sets Hash. Set Linked. Hash. Set Enum. Set Tree. Set DAST SORTED NULLS hash table no yes hash table + insertion order yes linked list bit vector red-black tree natural order sorted by comparator no depends

Set Performance Hash. Set Linked. Hash. Set Enum. Set ADD O(1) CONTAINS O(1) NEXT

Set Performance Hash. Set Linked. Hash. Set Enum. Set ADD O(1) CONTAINS O(1) NEXT O(h/n) O(1) Tree. Set O(log n)

Queues and Deques Queue Priority. Queue Deque Array. Deque Priority Queue = Heap •

Queues and Deques Queue Priority. Queue Deque Array. Deque Priority Queue = Heap • offer(e) • poll() • peek() Linked. List Deque as a Stack • add. First(e) stack push(e) • remove. First() stack pop() • peek. First() stack peek()

Queue Implementations Priority. Queue Array. Deque Linked. List DAST min heap array linked list

Queue Implementations Priority. Queue Array. Deque Linked. List DAST min heap array linked list SORTED comparator no no NULLS no no yes

Queue Performance Priority Queue OFFER O(log n) PEEK O(1) POLL O(log n) Linked List

Queue Performance Priority Queue OFFER O(log n) PEEK O(1) POLL O(log n) Linked List O(1) Array Deque O(1) O(1)

Map<K, V> Interface • • • V put(K key, V value) put. All(Map<? extends

Map<K, V> Interface • • • V put(K key, V value) put. All(Map<? extends K, ? extends V> m) V remove(Object key) clear() boolean contains. Key(Object key) boolean contains. Value(Object value) boolean is. Empty() int size() V get(Object key) Set<Map. Entry<K, V>> entry. Set() Set<K> key. Set() Collection<V> values()

Map Implementations Map Enum. Map Sorted. Map Identity. Hash. Map Navigable. Map Tree. Map

Map Implementations Map Enum. Map Sorted. Map Identity. Hash. Map Navigable. Map Tree. Map Weak. Hash. Map Linked. Hash. Map

Sorted Map • • • K first. Key() K last. Key() Sorted. Map<K, V>

Sorted Map • • • K first. Key() K last. Key() Sorted. Map<K, V> head. Map(K to) Sorted. Map<K, V> sub. Map(K from, K to) Sorted. Map<K, V> tail. Map(K from) Comparator<? super K> comparator()

Comparing Maps DAST Hash. Map hash table Linked. Hash. Map hash table + linked

Comparing Maps DAST Hash. Map hash table Linked. Hash. Map hash table + linked list SORTED NULLS no yes insertion order yes or access order Enum. Map Tree. Map natural order sorted by comparator array red-black tree no depends

Map Performance GET Hash. Map O(1) Linked. Hash. Map O(1) Enum. Map O(1) CONTAINS

Map Performance GET Hash. Map O(1) Linked. Hash. Map O(1) Enum. Map O(1) CONTAINS KEY O(1) NEXT O(h/n) O(1) Tree. Map O(log n)

Utilities Collection algorithms: • - min • - max • - frequency • -

Utilities Collection algorithms: • - min • - max • - frequency • - disjoint List algorithms: • - sort • - binary. Search • - reverse • - shuffle • - swap • - fill • - copy • - replace. All • - index. Of. Sub. List • - last. Index. Of. Sub. List Factories: • - EMPTY_SET • - EMPTY_LIST • - EMPTY_MAP • - empty. Set • - empty. List • - empty. Map • - singleton. List • - singleton. Map • - n. Copies Comparators: • - reverse. Order Wrappers: - unmodifiable. Collection - unmodifiable. Set - unmodifiable. Sorted. Set - unmodifiable. List - unmodifiable. Map - unmodifiable. Sorted. Map - synchronized * - checked *

More… • Java Generics and Collections book by Naftalin & Wadler (exists in the

More… • Java Generics and Collections book by Naftalin & Wadler (exists in the library) • Tutorial on-line by Josh Bloch • Cheat-sheet 2 -page PDF by coder-friendly • Various presentations and articles by Alex Miller (a. k. a. pure-danger-tech)