Hashing By Emily Nelson. The Official Definition Using a hash function to turn some kind of data in relatively small integers or Strings The “hash code”

Slides:



Advertisements
Similar presentations
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Advertisements

Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Hashing as a Dictionary Implementation
What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
Hashing. 2 Searching Consider the problem of searching an array for a given value –If the array is not sorted, the search requires O(n) time If the value.
Fall 2007CS 225 Sets and Maps Chapter 9. Fall 2007CS 225 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn.
Using Maps. A simple map: Hashtable To create a Hashtable, use: import java.util.*; Hashtable table = new Hashtable(); To put things into a Hashtable,
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
hashing1 Hashing It’s not just for breakfast anymore!
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Sets and Maps (and Hashing)
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.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
Hashing The Magic Container. Interface Main methods: –Void Put(Object) –Object Get(Object) … returns null if not i –… Remove(Object) Goal: methods are.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Recitation 1 CS0445 Data Structures Mehmud Abliz.
Hash Functions and the HashMap Class A Brief Overview On Green Marble John W. Benning.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Sets and Maps Starring: keySet Co-Starring: Collections.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Data structures Abstract data types Java classes for Data structures and ADTs.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
Computer Science 112 Fundamentals of Programming II Implementation Strategies for Unordered Collections.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Copyright © Curt Hill Hashing A quick lookup strategy.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Set and Map IS 313, Skeletons  Useful coding patterns  Should understand how and why they work how to use  possible quiz material.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Hashing O(1) data access (almost) -access, insertion, deletion, updating in constant time (on average) but at a price… references: Weiss, Goodrich & Tamassia,
Maps Nick Mouriski.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
1 Hashing Starring: HashSet Co-Starring: HashMap.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Collections Dwight Deugo Nesa Matic
Hashing. Searching Consider the problem of searching an array for a given value If the array is not sorted, the search requires O(n) time If the value.
Building Java Programs Generics, hashing reading: 18.1.
Sets and Maps Chapter 9.
Sections 10.5 – 10.6 Hashing.
Hashing.
Efficiency of in Binary Trees
Week 8 - Wednesday CS221.
Efficiency add remove find unsorted array O(1) O(n) sorted array
Hashing.
Part of the Collections Framework
Collections Framework
Hashing.
L5. Necessary Java Programming Techniques
Sets and Maps Chapter 9.
Chapter 8 The Map ADT.
Hashing in java.util
Collision Handling Collisions occur when different elements are mapped to the same cell.
Hashing.
Data Structures II AP Computer Science
Hashing.
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Hashing By Emily Nelson

The Official Definition Using a hash function to turn some kind of data in relatively small integers or Strings The “hash code” is used to store the data in a data structure such as a hashtable Hashing is used to greatly speed up data base lookups

How I Like To Think About It Hash function Data:Hashcode: cheese The beautiful bay horse I love AP Computer Science! D56TF2A QP78T43 Z5TY90

In Other Words Your object enters the hash function and gets scrambled. That scrambled data becomes the name of the data slot where the object is stored. When you need access, you can just go straight to the right data slot, no searching required!

A Good Hash Function A good hash function is…A good hash function is… –Fast to calculate –Deterministic: the same value will give the same hashcode if hashed twice –Uniform: the hash values are spread over the entire range, not clustered –Continuous: data that is very similar should produce a similar (but not identical) hashcode –And most importantly… A good hash function minimizes collisions!

Collisions

What Is A Collision? A collision is when two pieces of data hash to the same slotA collision is when two pieces of data hash to the same slot How a hash function resolves collisions is very important.How a hash function resolves collisions is very important. Some ways of doing this:Some ways of doing this: –Rehash the hash value –Move sequentially through the slots until you find an empty one –Use a linked list and create “buckets” at each slot.

Watch This… Data: Hash Table: cheese Hash function cheeseburger Coca Cola Collision!

How To Fix It cheese cheeseburger Sequential Search Coca cola Can’t go here Try here SUCCESS! Think of this technique as a ladder; if the current rung doesn’t work, try the next one down. *Inefficient when hashtable is nearly full.

How To Fix It cheese cheeseburger Hash and Rehash Coca Cola Can’t go here SUCCESS! Some algorithms rehash the original data with the hash value concatenated onto it. If it doesn’t work the 2 nd time, rehash!

How To Fix It cheese cheeseburger Linked List Coca cola When you use this technique, the slots in the memory are now called “buckets.” *Inefficient if space is a problem or if the hash value assigns too much data to the same bucket.

Efficiency Two definitions you should know: initial capacity and load factor The initial capacity of a hash table is how much it can hold when it is initialized. This is automatically increased by the computer. The load factor of the hash table is how full the hash table is allowed to get before the capacity is increased. A good load factor that balances time and space requirements is about.75.

Why We Should LOVE The Guys At Sun Microsystems… &

HashSet A HashSet –Contains no duplicate elements –Has no guarantee to the iteration order of the set –Implements Comparable, Set, and Collection –Extends AbstractSet

OK, Here’s How I Think About It… Hash function HashSet Cheese Pizza Pepperoni Cheese Data: No Duplicate Elements!

The HashSet is like a bag… You drop stuff (data) into it… And when you dump it out… You can’t control the order it comes out.

Methods of HashSet HashSet() –Constructor; creates a new HashSet add(Object o) –Adds the specified object to the set IF it is not already present, returns true if successful clear() –Clears all data in the set contains(Object o) –Returns true if the object contains the specificed object

isEmpty() –Returns true if there are no objects in the set iterator() –Returns an Interator over this set remove(Object o) –Removes the specified object and returns true size() –Returns the number of elements in this set More Methods of HashSet

HashMap Maps keys to valuesMaps keys to values No guarantee for interator orderNo guarantee for interator order Implements MapImplements Map Extends AbstractMapExtends AbstractMap

Here’s Another Way to Think About It… HashMap Data (key, value): red blue green The HashMap hashes and stores your data, and keeps a “key” to the location, like a signpost. To find your data, you need the key!

Methods of HashMap HashMap() –Constructor; creates a new HashMap clear() –Removes all the data in the Map containsKey(Object key) –Returns true if the Map contains a mapping for the specified key containsValue(Object value) –Returns true if the Map contains one or more keys mapped to this value

More Methods of HashMap entrySet() –Returns a Set with all the mappings contained in the Map get(Object key) –Returns the object to which this key is mapped; if no mapping, returns null isEmpty() –Returns true if there are no mappings in the Map keySet() –Returns a Set with all the keys contained in the Map

Yet More Methods of HashSet put(Object key, Object value) –Places the specified mapping in the Map; returns the inserted object remove(Object key) –Removes the mapping for this key; returns the removed object size() –Returns the number of key-value mappings values() –Returns a Collection of all of the values in the map

DF56HW390JKH8 Good Luck!