Presentation is loading. Please wait.

Presentation is loading. Please wait.

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”

Similar presentations


Presentation on theme: "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”"— Presentation transcript:

1 Hashing By Emily Nelson

2 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

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

4 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!

5 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!

6 Collisions

7 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.

8 Watch This… Data: Hash Table: 1 2 3 4 5 6 7 8 9 10 cheese Hash function cheeseburger Coca Cola Collision!

9 How To Fix It 1 2 3 cheese 4 5 6 cheeseburger 7 8 9 10 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.

10 How To Fix It 1 2 3 cheese 4 5 6 cheeseburger 7 8 9 10 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!

11 How To Fix It 1 2 3 cheese 4 5 6 cheeseburger 7 8 9 10 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.

12 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.

13 Why We Should LOVE The Guys At Sun Microsystems… &

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

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

16 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.

17 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

18 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

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

20 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!

21 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

22 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

23 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

24 DF56HW390JKH8 Good Luck!


Download ppt "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”"

Similar presentations


Ads by Google