Java Reference
In-Depth Information
43
44 this .loadFactorThreshold = loadFactorThreshold;
45 table = new LinkedList[capacity];
46 }
47
48 @Override /** Remove all elements from this set */
49 public void clear() {
50 size = 0 ;
51 removeElements();
52 }
53
54 @Override /** Return true if the element is in the set */
55 public boolean contains(E e) {
56 int bucketIndex = hash(e.hashCode());
57 if (table[bucketIndex] != null ) {
58 LinkedList<E> bucket = table[bucketIndex];
59
clear
contains
for (E element: bucket)
60
if (element.equals(e))
61
return true ;
62 }
63
64
return false ;
65 }
66
67 @Override /** Add an element to the set */
68
public boolean add(E e) {
add
69
if (contains(e)) // Duplicate element not stored
70
return false ;
71
72
if (size + 1 > capacity * loadFactorThreshold) {
73
if (capacity == MAXIMUM_CAPACITY)
74
throw new RuntimeException( "Exceeding maximum capacity" );
75
76 rehash();
77 }
78
79
int bucketIndex = hash(e.hashCode());
80
81 // Create a linked list for the bucket if not already created
82 if (table[bucketIndex] == null ) {
83 table[bucketIndex] = new LinkedList<E>();
84 }
85
86 // Add e to hashTable[index]
87 table[bucketIndex].add(e);
88
89 size++; // Increase size
90
91
return true ;
92 }
93
94 @Override /** Remove the element from the set */
95
public boolean remove(E e) {
remove
96
if (!contains(e))
97
return false ;
98
99
int bucketIndex = hash(e.hashCode());
100
101
// Create a linked list for the bucket if not already created
102
if (table[bucketIndex] != null ) {
 
Search WWH ::




Custom Search