big java chapter 16. advanced data structures often referred to as the java collections...

16
Big Java Big Java Chapter 16 Chapter 16

Upload: naomi-carter

Post on 13-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Big JavaBig JavaChapter 16Chapter 16

Page 2: 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

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

Page 3: 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

SetsSets

• Unordered collection• Fundamental operations:

– Add an element– Remove an element– Containment test (is object in set?)– List elements (in arbitrary order)

• Reject duplicates

Page 4: 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

Set ImplementationSet Implementation

<<interface>>Set

HashSet TreeSet

Page 5: 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

ExampleExample

instantiate with specificimplementation.Advantage: could easilychange implementation,rest of program stays thesame.

use methods from interface

could also do:if (names.contains(“Jane”)) { … }

Page 6: 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

Example (continued)Example (continued)

can use “for each”loop with Sets

Nodes are notnecessarily visitedin order inserted!

Page 7: 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

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?

Page 8: 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

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.

Page 9: 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

ExampleExample

Page 10: 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

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

Page 11: 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

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.

Page 12: 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

Binary Search TreeBinary Search Tree

• Review on your own, pages 720-734

Page 13: 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

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

Page 14: 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

Reading AssignmentReading Assignment

• Random Fact 16.2, Software Piracy, pages 738-739

Page 15: 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 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))

Page 16: 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

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