Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Integral Keys

Out of all the Java primitive types, the so-called integral types  are the simplest to hash into integers. The integral data types are byte, short, int, long, and char. Since the underlying representation of such data types can be viewed as an integer, the hash function is trivial. A suitable function f for an integral data type is the identity function:

displaymath61474

Program gif completes the definition of the Int wrapper class introduced in Program gif. In this case, the hashCode method simply returns the contents of the value field. Clearly, the running time of of the hashCode method is O(1).

   program10638
Program: Int class hashCode method.


next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.