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.
Java Methods Object-Oriented Programming and Data Structures 2nd AP edition with GridWorld Maria Litvin ● Gary Litvin Chapter Hash Function Danger Keep Out 25 Lookup Tables and Hashing Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All rights reserved Objectives: • Learn about lookup tables • Learn about hashing • Review java.util.HashSet and java.util.HashMap 252 Lookup Tables • A lookup table is an array that helps to find data very quickly • The array stores references to data records (or some values) • A data record is identified by some key • The value of a key is directly translated into an array index using a simple formula 253 Lookup Tables (cont’d) • Only one key can be mapped onto a particular index (no collisions) • The index that corresponds to a key must fall into the valid range (from to array.length-1) • Access to data is “instantaneous” (O(1)) 254 Lookup Tables: Example Zip codes 600 601 1004 1005 1006 1007 1008 1009 99950 99951 99998 99999 Corresponding locales Adjuntas, PR Amherst, MA Barre, MA Belchertown, MA Blanford, MA Bondsville, MA Some table entries remain unused Ketchikan, AK 255 Lookup Tables: Example private static final int [ ] n_thPowerOf3 = { 1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683 }; // precondition: