TreeMap & HashMap 7/16/2009.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Appendix I Hashing. Chapter Scope Hashing, conceptually Using hashes to solve problems Hash implementations Java Foundations, 3rd Edition, Lewis/DePasquale/Chase21.
Maps, Dictionaries, Hashtables
Sets and Maps Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Classes and objects Practice 2. Basic terms  Classifier is an element of the model, which specifies some general features for a set of objects. Features.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
1 Lecture 12 More on Hashing Multithreading. 2 Speakers!
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
Big Java Chapter 16.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
LECTURE 34: MAPS & HASH CSC 212 – Data Structures.
CSC 427: Data Structures and Algorithm Analysis
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 Space vs. time  space/time tradeoffs  hashing  hash table, hash function  linear probing.
COSC 1030 Lecture 10 Hash Table. Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Data Structure & Algorithm Lecture 8 – Hashing JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Java Collections CHAPTER 3-February-2003
CS203 Lecture 14. Hashing An object may contain an arbitrary amount of data, and searching a data structure that contains many large objects is expensive.
1 CSC 321: Data Structures Fall 2012 Hash tables  HashSet & HashMap  hash table, hash function  collisions  linear probing, lazy deletion, primary.
Appendix I Hashing.
Sets and Maps Chapter 9.
CSC 321: Data Structures Fall 2013 Hash tables HashSet & HashMap
Hashing.
11 Map ADTs Map concepts. Map applications.
Hashing & HashMaps CS-2851 Dr. Mark L. Hornick.
TCSS 342, Winter 2006 Lecture Notes
Chapter 19 Java Data Structures
Week 8 - Wednesday CS221.
CSC 427: Data Structures and Algorithm Analysis
Data Structures TreeMap HashMap.
Dictionaries Dictionaries 07/27/16 16:46 07/27/16 16:46 Hash Tables 
Dictionaries 9/14/ :35 AM Hash Tables   4
Searching.
Chapter 10: Non-linear Data Structures
7/23/2009 Many thanks to David Sun for some of the included slides!
TreeSet TreeMap HashMap
Hash Tables -The Hacker's Dictionary
Hashing CS2110 Spring 2018.
Hashing II CS2110 Spring 2018.
Graphs (picture above from ) 7/29/2009
Hashing CS2110.
Chapter 10 Hashing.
CSE 373: Data Structures and Algorithms
Teach A level Computing: Algorithms and Data Structures
Chapter 21 Hashing: Implementing Dictionaries and Sets
Maps.
slides created by Marty Stepp and Hélène Martin
slides adapted from Marty Stepp and Hélène Martin
Implementing Hash and AVL
CS2110: Software Development Methods
Binary Search Trees 7/16/2009.
Hash Tables Computer Science and Engineering
CSE 1020: The Collection Framework
Sets and Maps Chapter 9.
Hash Tables By JJ Shepherd.
Hashing in java.util
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Trees in java.util A set is an object that stores unique elements
slides created by Marty Stepp and Hélène Martin
Mobile genetic elements associated with bla OXA genes.
Presentation transcript:

TreeMap & HashMap 7/16/2009

Important Dates Project 2 due Midterm Review Midterm 2 DESIGN, DESIGN, DESIGN!!! You may have 0 or 1 partner. NO EXCEPTIONS! Due Friday 7/24/2009 – 10pm Midterm Review Saturday 7/26/2009 – 1-4pm in 306 Soda Midterm 2 Tuesday 7/28/2009 – 5-6pm in 10 Evans Important Dates

Map is an Interface KEY VALUE Jonathan Blue Kaushik Green David George Angela Orange Stephanie Yellow

Remember the Person class?

We want to use it in a TreeMap! Implement Comparable!

Using java.util.TreeMap “Entry” or “Node” Person key String value Person String myName “Jon” “Blue”

myLeft myRight myLeft myRight myLeft myRight

main put Needs to figure out where the key, value should go Creates a HashTree Calls put with a key and a value put Needs to figure out where the key, value should go Uses compareTo()to find the spot in the tree

What was the point? get: returns the value for that key put: takes a key and a value and adds them to the map. containsKey: returns a boolean KEY VALUE Jonathan Blue Kaushik Green David George Angela Orange Stephanie Yellow

What if our keys were 2 letter words and our values their definitions? aa Rough, cindery lava ab Abdominal muscle ad Advertisement ae One ag Pertaining to agriculture ah To exclaim in amazement ai Three-toed sloth al East Indian tree …

TreeMap to represent 2 letter words & def. KEY VALUE aa Rough, cindery lava ab Abdominal muscle ad Advertisement ae One ag Pertaining to agriculture ah To exclaim in amazement ai Three-toed sloth al East Indian tree … TreeMap to represent 2 letter words & def. ae ab aa ad ah ag ai

TreeMap to represent 2 letter words and definitions (approximate picture) Could we do better? 26*26 = 676 nodes (height of 10)

Use a 676 element array! String word = “aa” 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb bc Bd “East Indian Tree” “Rough, cindery lava” String word = “aa” 26 * (word.charAt(0) - 'a') + (word.charAt(1) - 'a');

What are problems with the array representation? If we’re storing only 5 entries it is a lot of wasted space! If we want to store more than 2 letter words it would get huge quickly! We can do better!!! Using a HashTable! Or using java.util.HashMap

main Creates a HashMap Calls put with a key and a value put Needs to figure out where the key, value should go Calls the hashCode()function on the key Takes the return value and reduces it with mod to get an index hashcode Calculates a hashCode value for the object Identical object MUST return the same value for hashCode()

Make Person work in a HashMap Override equals(Object obj) Provide hashCode()

1 2 3 4 5 6 7 8 9 Creates a HashMap Calls put with a key and a value 1 2 3 4 5 6 7 8 9 main Creates a HashMap Calls put with a key and a value put Needs to figure out where the key, value should go Calls the hashCode()function on the key Takes the return value and reduces it with mod to get an index hashcode Calculates a hashCode value for the object Identical object MUST return the same value for hashCode()

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

main Creates a HashMap Calls put with a key and a value put Needs to figure out where the key, value should go Calls the hashCode()function on the key Takes the return value and reduces it with mod to get an index hashcode Calculates a hashCode value for the object Identical object MUST return the same value for hashCode()

Sometimes we have collisions In the example in lecture we created a LinkedList when we had a collision (Chaining) You can have a collision in two ways. Both objects return exactly the same value from the function hashCode() When we get the value from hashCode() we need to calculate what bucket to put it in. We use a “compression-function” or in our simple case mod. The compression function can return the same number for two different hashCodes

Hashing Summary In class Object, is function hashCode() Rule: Inherited by every class. By default, returns address of this. Can override it for your particular type. Rule: If x.equals(y) then x.hashCode() == y.hashCode(). The inverse need not to be true. The types Hashtable, HashSet, and HashMap use hashCode to give you fast look-up of objects.

“Entry” or “Node” Person Person key String myName “Blue” “George” String value Person String myName “Blue” “George”