Java Reference
In-Depth Information
38
return false
;
39 }
40
41 /**
42
Adds an element to this set.
43 @param x
an object
44 @return
true if x is a new object, false if x was
45
already in the set
46 */
47
public boolean
add(Object x)
48 {
49
int
h = x.hashCode();
50
if
(h <
0
) h = Ċh;
51 h = h % buckets.length;
52
53 Node current = buckets[h];
54
while
(current !=
null
)
55 {
56
if
(current.data.equals(x))
57
return false
;
//
Already in the set
58 current = current.next;
59 }
60 Node newNode =
new
Node();
61 newNode.data = x;
62 newNode.next = buckets[h];
63 buckets[h] = newNode;
64 size++;
65
return true
;
66 }
67
68 /**
69
Removes an object from this set.
70 @param x
an object
71 @return
true if x was removed from this set, false
72
if
x
was not an element of this set
73 */
74
public boolean
remove(Object x)
75 {
76
int
h = x.hashCode();
77
if
(h <
0
) h = Ċh;
78 h = h % buckets.length;
711
712