Chapter 25 - Lookup tables and hashing. After you have mastered the material in this chapter, you will be able to: Learn about lookup tables, learn about hashing, review java.util.HashSet and java.util.HashMap.
Trang 1Lookup Tables and Hashing
Hash Function
Danger Keep Out
25 Chapter
Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All
rights reserved.
Java Methods
Object-Oriented Programming
and Data Structures
Maria Litvin ● Gary Litvin
2nd AP edition with GridWorld
Trang 2Objectives:
java.util.HashMap
Trang 3Lookup Tables
data very quickly
(or some values)
an array index using a simple formula
Trang 4Lookup Tables (cont’d)
particular index (no collisions).
Trang 6Lookup Tables: Example 2
private static final int [ ] n_thPowerOf3 =
{ 1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683 }; .
Trang 7Lookup Tables: Example 3
256 colors used
in a particular image; each of the palette entries corresponds to a triplet of RGB values
Trang 9Hash Tables
the valid range
onto the same array index — this situation is
Trang 10Hash Tables (cont’d)
the array indices randomly and uniformly
minimize the number of collisions
resolving collisions: chaining and probing.
Trang 11Danger Keep Out
Trang 12Danger Keep Out
computed using a certain probing formula
Trang 13using hash tables (with chaining)
Trang 14small
Load factor = Total number of items
Number of buckets
Trang 15iterations over the whole set
the table is automatically rehashed into a
larger table; if possible this should be
avoided
Trang 16helps calculate the hashing function
range of indices in a particular hash table
Trang 18should agree with each other:
x.equals (y) x.compareTo (y) == 0
x.equals (y) x.hashCode( ) == y.hashCode( )
Trang 19Never mind
Trang 202520
Trang 21Review:
table and a hash table?
Trang 22Review (cont’d):
the load factor is too high? Too low?
when the load factor exceeds the specified
limit?
capacity to 16 and the load factor limit to
0.75 How many values can be stored in this table before it is rehashed?
Trang 23Review (cont’d):
method?