big java chapter 16. advanced data structures often referred to as the java collections...
TRANSCRIPT
Big JavaBig JavaChapter 16Chapter 16
Advanced Data StructuresAdvanced Data Structures
Often referred to as the Java Collections Framework….
• Set and map data types• Hash tables• Binary trees• Heap• Priority queue
SetsSets
• Unordered collection• Fundamental operations:
– Add an element– Remove an element– Containment test (is object in set?)– List elements (in arbitrary order)
• Reject duplicates
Set ImplementationSet Implementation
<<interface>>Set
HashSet TreeSet
ExampleExample
instantiate with specificimplementation.Advantage: could easilychange implementation,rest of program stays thesame.
use methods from interface
could also do:if (names.contains(“Jane”)) { … }
Example (continued)Example (continued)
can use “for each”loop with Sets
Nodes are notnecessarily visitedin order inserted!
Reading AssignmentReading Assignment
• Read Quality Tip 16.1 pages 704-705. How would you feel if you had designed the List interface and ArrayList/LinkedList classes?
MapsMaps• 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.
ExampleExample
Hash TableHash 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
HashSetHashSet• 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; //primeint 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 TreeBinary Search Tree
• Review on your own, pages 720-734
TreeSet or HashSet?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 AssignmentReading Assignment
• Random Fact 16.2, Software Piracy, pages 738-739
Priority QueuePriority 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 ExerciseChapter 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