Chapter 10.2-10.4 Generic Programming and Collection Classes Lists • ArrayList • • • • implements as a dynamic array. creates new array and copies when array fills up. Very fast to randomly access slow to insert and delete • LinkedList • more efficient in applications where items will often be added or removed at the beginning of the list or in the middle of the list. List methods • For all lists: • get(index), set(index,obj), add(index,obj), remove(index), indexOf(obj) • for LinkedLists: • getFirst(), getLast(), removeFirst(), removeLast(), .addFirst(obj), addLast(obj) 10.2.2 Sorting • Collections.sort(list); • For List<T>, requires that t implements Comparable<T> • Collections.sort(list,comparator); • Comparator is an object that defines a compare() method that can be used to compare two objects. • Uses “Merge sort” with both average and worstcase of n*log(n) • A couple useful methods: • Collections.shuffle(list) • Collections.reverse(list) 10.2.3 TreeSet and HashSet • A set is a collection of objects in which no object occurs more than once. • java.util.TreeSet • always held in ascending order • requires Comparable<T> • java.util.HashSet • stores its elements in a hash table • finding, adding, and removing efficient • not stored in any particular order Set Methods 10.2.4 EnumSet • read it if you like, but we’ll skip this one 10.2.5 Priority Queues • This looks pretty advanced, we’ll skip this one too 10.3 Maps • An array of N elements can be thought of as a way of associating some item with each of the integers 0, 1, . . . , N-1. If i is one of these integers, it’s possible to get the item associated with i, and it’s possible to put a new item in the i-th position. These “get” and “put” operations define what it means to be an array. • A map is a kind of generalized array. Like an array, a map is defined by “get” and “put” operations. But in a map, these operations are defined not for integers 0, 1, . . . , N-1, but for arbitrary objects of some specified type T. Associated to these objects of type T are objects of some possibly different type S. Map Interface • get(key), put(key,value), putAll(map2), remove(key), containsKey(key), containsValue(value), size(), isEmpty(), clear() Classes that implement Map • TreeMap<K,V> • the key/value associations are stored in a sorted tree, in which they are sorted according to their keys. • Must be Comparable<K> • HashMap<K,V> • does not store associations in any particular order • must have equals() and hashCode() methods 10.3.2 Views, SubSets, and SubMaps • Looks like pretty deep reference material 10.3.3 Hash Tables and Hash Codes 10.4 Programming with the Java Collection Framework • Some good reading here. • Give it a skim • Use it for reference.