Java Reference
In-Depth Information
ch16/hashtable/HashSet.java
1 import java.util.AbstractSet;
2 import java.util.Iterator;
3 import java.util.NoSuchElementException;
4
5 /**
6 A hash set stores an unordered collection of objects, using
7 a hash table.
8 */
9 public class HashSet extends AbstractSet
10 {
11 /**
12 Constructs a hash table.
13 @param bucketsLength the length of the buckets array
14 */
15 public HashSet( int bucketsLength)
16 {
17 buckets = new Node[bucketsLength];
18 size = 0 ;
19 }
20
21 /**
22 Tests for set membership.
23 @param x an object
24 @return true if x is an element of this set
25 */
26 public boolean contains(Object x)
27 {
28 int h = x.hashCode();
29 if (h < 0 ) h = Ċh;
30 h = h % buckets.length;
31
32 Node current = buckets[h];
33 while (current ! = null )
34 {
35 if (current.data.equals(x)) return
true ;
36 current = current.next;
37 }
710
711
Search WWH ::




Custom Search