Big Java Chapter 16
Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary trees Heap Priority queue
Sets Unordered collection Fundamental operations: Reject duplicates Add an element Remove an element Containment test (is object in set?) List elements (in arbitrary order) Reject duplicates
<<interface>> Set Implementation <<interface>> Set HashSet TreeSet
Example instantiate with specific implementation. Advantage: could easily change implementation, rest of program stays the same. use methods from interface could also do: if (names.contains(“Jane”)) { … }
Example (continued) can use “for each” loop with Sets Nodes are not necessarily visited in order inserted!
Reading Assignment Read Quality Tip 16.1 pages 704-705. How would you feel if you had designed the List interface and ArrayList/LinkedList classes?
Maps A map data type keeps associations between keys and values Cannot contain duplicate keys Operations include: put get containsKey/containsValue keySet/values – return all keys/values size Java has two implementations: HashMap and TreeMap.
Example
Hash Table Hash table – can be used to implement sets and maps Hash function – computes an integer value (hash code) from an object, goal is for different objects to yield different hash codes int h = obj.hashCode(); hashCode is inherited from Object Collision – when two or more distinct objects have the same hash code
HashSet Author provides HashSet, an extension of AbstractSet provides a hash table with buckets (linked lists) to hold collisions possible hash code for string: final int HASH_MULTIPLER = 31; //prime int h=0; for (int i=0; i<s.length(); i++) h = HASH_MULTIPLIER * h + s.charAt(i); Can use integer fields directly as hash codes If hashCode not overridden, Object class computes based on memory location of object. Problem if define equals but not hashCode.
Binary Search Tree Review on your own, pages 720-734
TreeSet or HashSet? With a good hash function, hashing is usually faster Balanced trees (remember those?) can guarantee reasonable performance, HashSet depends entirely on performance of hash function To use TreeSet, objects being stored must implement Comparable interface For TreeMap, same requirement for keys Can supply a Comparator object to TreeSet/TreeMap constructor (takes two objects and returns comparison) TreeSet is preferable if want to print list of items in order
Reading Assignment Random Fact 16.2, Software Piracy, pages 738-739
Priority Queue Collects elements which have a priority Elements inserted in any order, but retrieved according to priority Java includes PriorityQueue() class Abstract data class Often use heap to implement Heaps covered in 406 (I think) Heapsort is O(n log(n))
Chapter 16 Quick Exercise Use the HashTable code from the textbook Can you store objects with the same hash code but different data? Try modifying CoinHashCodePrinter to insert two quarters but initialize them differently (e.g., “a quarter” and “quarter” rather than both “quarter”) Review the TreeSet and TreeSetTester code. Notice the use of CoinComparator. Modify to use with some other class, such as BankAccount