-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashSet.java
More file actions
74 lines (69 loc) · 1.94 KB
/
HashSet.java
File metadata and controls
74 lines (69 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.*;
/**
* A simplified implementation of hash sets instrumented to count the number
* of collisions. Unlike {@link java.util.HashSet}, the capacity of this table
* never grows beyond its initial capacity.
*
* @param <T> the type of object stored in the set
*/
public class HashSet<T> {
private final int capacity;
private final List<T>[] elements;
private int numCollisions;
/**
* Constructs a hash set with the specified capacity (number of
* buckets).
*
* @param capacity the capacity
*/
public HashSet(int capacity) {
this.capacity = capacity;
elements = (List<T>[]) (new List[capacity]);
numCollisions = 0;
}
/**
* Adds the specified value to the hash set if it is not already present.
*
* @param value the value to add
*/
public void add(T value) {
if (contains(value)) {
return;
}
int index = Math.abs(value.hashCode() % capacity);
if (elements[index] == null) {
elements[index] = new LinkedList<T>();
} else {
numCollisions++;
}
elements[index].add(value);
}
/**
* Tests whether the hash set contains the specified value.
*
* @param value the value
* @return true if the value is in the set, false otherwise
*/
public boolean contains(T value) {
int index = Math.abs(value.hashCode() % capacity);
List<T> bucket = elements[index];
if (bucket == null) {
return false;
}
for (T item : bucket) {
if (item.equals(value)) {
return true;
}
}
return false;
}
/**
* Gets the number of times distinct values hashed to the same
* bucket during calls of {@link #add(Object)}.
*
* @return the number of collisions
*/
public int getNumCollisions() {
return numCollisions;
}
}