Java Reference
In-Depth Information
String[] fruits = {"apples", "pears", "grapes", "ba-
nanas", "kiwis"};
int[] quantities = {10, 15, 8, 17, 30};
for (int i = 0; i < fruits.length; i++)
msi.put(fruits[i], quantities[i]);
When you run this application, it generates the following output:
apples: 10
bananas: 17
grapes: 8
kiwis: 30
pears: 15
The HashMap class provides a map implementation that is based on a hashtable data
structure. This implementation supports all Map operations, and permits null keys and
null values. It makes no guarantees on the order in which entries are stored.
A hashtable maps keys to integer values with the help of a hash function . Java
provides this function in the form of Object 's hashCode() method, which classes
override to provide appropriate hash codes.
A hash code identifies one of the hashtable's array elements, which is known as a
bucket or slot . For some hashtables, the bucket may store the value that is associated
with the key. Figure 5-3 illustrates this kind of hashtable.
Search WWH ::

Custom Search