Java Reference
In-Depth Information
Display 15.34
A Hash Table Class (part 2 of 2)
9
for ( int i=0; i < SIZE; i++)
10
hashArray[i] = new LinkedList2( );
11
}
12
private int computeHash(String s)
13
{
14
int hash = 0;
15
for ( int i = 0; i < s.length( ); i++)
16
{
17
hash += s.charAt(i);
18
}
19
return hash % SIZE;
20
}
21
/**
22
Returns true if the target is in the hash table,
23
false if it is not.
24
*/
25
public boolean containsString(String target)
26
{
27
int hash = computeHash(target);
28
LinkedList2 list = hashArray[hash];
29
if (list.contains(target))
30
return true ;
31
return false ;
32
}
33
/**
34
Stores or puts string s into the hash table
35
*/
36
public void put(String s)
37
{
38
int hash = computeHash(s); // Get hash value
39
LinkedList2 list = hashArray[hash];
40
if (!list.contains(s))
41
{
42
// Only add the target if it's not already
43
// on the list.
44
hashArray[hash].addToStart(s);
45
}
46
}
47
} // End HashTable class
 
Search WWH ::




Custom Search