Java Collections OOP tirgul No. 7 2009
History JDK 1.0: Vector, Dictionary, Hashtable, Stack, Enumeration JDK 1.2: Collection, Iterator, List, Set, Map, ArrayList, HashSet, TreeSet, HashMap, WeakHashMap JDK 1.4: RandomAccess, IdentityHashMap, LinkedHashMap, LinkedHashSet JDK 1.5: Queue, java.util.concurrent, ... JDK 1.6: Deque, ConcurrentSkipListSet/Map, ... JDK 1.7: TransferQueue, LinkedTransferQueue
public interface Collection<E>/Map<K,V> Collection = a group of objects public interface Collection<E>/Map<K,V> How to choose right collection? Implementation Interface Duplicates? List, Queue Set Map - + Complexity? Add/remove/search O(1)/O(LogN)/O(N) Sorted? SortedSet SortedMap + Requirements? equals() hashCode() Comparable Keys Map Set - + Null support? Synchronized?
Collection<E> Map<K,V> Set<E> List<E> 7 Basic collection interfaces… Iterable<E> Collection<E> Map<K,V> Set<E> List<E> Queue<E> SortedMap<K,V> SortedSet<E>
Collection<E> interface: Basic, general, flexible operations to retrieve/add/remove members public interface Collection<E> extends Iterable<E> { // Basic operations int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator(); // Bulk operations boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); //optional boolean removeAll(Collection<?> c); //optional boolean retainAll(Collection<?> c); //optional void clear(); //optional // Array operations Object[] toArray(); <T> T[] toArray(T[] a); }
Map<K,V> interface: Access to values using keys public interface Map<K,V> { // Basic operations V put(K key, V value); V get(Object key); V remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value); int size(); boolean isEmpty(); // Bulk operations void putAll(Map<? extends K, ? extends V> m); void clear(); // Collection Views Set<K> keySet(); Collection<V> values(); Set<Map.Entry<K,V>> entrySet(); // Interface for entrySet elements interface Entry { K getKey(); V getValue(); V setValue(V value); }
Queue & List Interfaces Ordered sequences of objects public interface List<E> extends Collection<E> { // Positional access E get(int index); E set(int index, E element); //optional boolean add(E element); //optional void add(int index, E element); //optional E remove(int index); //optional boolean addAll(int index, Collection<? extends E> c); //optional // Search int indexOf(Object o); int lastIndexOf(Object o); // Iteration ListIterator<E> listIterator(); ListIterator<E> listIterator(int index); // Range-view List<E> subList(int from, int to); } public interface Queue<E> extends Collection<E> { E element(); boolean offer(E e); E peek(); E poll(); E remove(); - The ‘getTextureString’ is just an illustration
Basic implementations LinkedHashMap TreeMap HashMap Map LinkedList ArrayList List LinkedHashSet TreeSet HashSet Set Interfaces Hash Table + Linked List Linked List Balanced Tree Resizable Array Hash Table Implementations Conventions equals+ hashCode equals compareTo
Computational complexity Mind the constants!!!
Computational complexity Add Remove Get Contains ArrayList O(1) O(N) LinkedList HashSet O(1) avg TreeSet O(logN)
Iterators • 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
Implementing new collection size… …isEmpty… …removeAll… …contains… …iterator… …add… …containsAll…… …remove Too many useful functions in interfaces? Abstract classes! AbstractCollection<E> AbstractList<E> AbstractMap<K,V> AbstractSet<E>
Working with collections import java.util.collections; Static library with many useful algorithms Searching… int pos = Collections.binarySearch(list, key); Counting… int frequency = Collections.frequency(myColl,item) ; Shuffling, sorting, reversing, performing set operations and much more…
Collection algorithms: - min - max - frequency - disjoint Factories: - EMPTY_SET - EMPTY_LIST - EMPTY_MAP - emptySet - emptyList - emptyMap - singleton - singletonList - singletonMap - nCopies - list(Enumeration) Comparators: - reverseOrder Miscellaneous: - addAll - enumeration Wrappers: - unmodifiableCollection - unmodifiableSet - unmodifiableSortedSet - unmodifiableList - unmodifiableMap - unmodifiableSortedMap - synchronizedCollection - synchronizedSet - synchronizedSortedSet - synchronizedList - synchronizedMap - synchronizedSortedMap - checkedCollection - checkedSet - checkedSortedSet - checkedList - checkedMap - checkedSortedMap Collection algorithms: - min - max - frequency - disjoint List algorithms: - sort - binarySearch - reverse - shuffle - swap - fill - copy - replaceAll - indexOfSubList - lastIndexOfSubList Collection factories: - EMPTY_SET - EMPTY_LIST - EMPTY_MAP - emptySet - emptyList - emptyMap - singleton - singletonList - singletonMap - nCopies list(Enumeration) Collection Wrappers: - unmodifiableCollection - unmodifiableSet - unmodifiableSortedSet - unmodifiableList - unmodifiableMap - unmodifiableSortedMap - synchronizedCollection - synchronizedSet - synchronizedSortedSet - synchronizedList - synchronizedMap - synchronizedSortedMap - checkedCollection - checkedSet - checkedSortedSet - checkedList - checkedMap - checkedSortedMap
More info and more operations: Exploring the code… (in Eclipse with JRE installed) Type collection name Ctrl-Left-mouse View More info and more operations: java API documentation Java Collection Tutorial http://java.sun.com/docs/books/tutorial/collections/index.html