@@ -67,7 +67,7 @@ public HashSet() {
67
67
* @return the number of elements in this set (its cardinality)
68
68
*/
69
69
public int size () {
70
- return this . size ;
70
+ return size ;
71
71
}
72
72
73
73
/**
@@ -76,13 +76,12 @@ public int size() {
76
76
* @return true if this set contains no elements
77
77
*/
78
78
public boolean isEmpty () {
79
- return this . size () == 0 ;
79
+ return size () == 0 ;
80
80
}
81
81
82
82
/**
83
- * TODO formal documentation.
84
83
* Simple hash function to hash the element into their respective bucket.
85
- * Currently uses the division method (k % m).
84
+ * Currently, uses the division method (k % m).
86
85
* T must override both Object::equals and Object::hashCode.
87
86
*
88
87
* @param element the specified element to be hashed.
@@ -104,12 +103,12 @@ private int hashFunction(T element) {
104
103
* element
105
104
*/
106
105
public boolean add (T element ) {
107
- int bucket = this . hashFunction (element );
108
- LinkedList <T > bucketLinkedList = this . buckets [bucket ];
106
+ int bucket = hashFunction (element );
107
+ LinkedList <T > bucketLinkedList = buckets [bucket ];
109
108
if (bucketLinkedList .search (element ) != -1 ) {
110
109
return false ; // element is already in the set.
111
110
}
112
- ++this . size ; // updates the cardinality of this hashset.
111
+ ++size ; // updates the cardinality of this hashset.
113
112
return bucketLinkedList .insertFront (element );
114
113
}
115
114
@@ -120,8 +119,8 @@ public boolean add(T element) {
120
119
* @return true if this set contains the specified element
121
120
*/
122
121
public boolean contains (T element ) {
123
- int bucket = this . hashFunction (element );
124
- LinkedList <T > bucketLinkedList = this . buckets [bucket ];
122
+ int bucket = hashFunction (element );
123
+ LinkedList <T > bucketLinkedList = buckets [bucket ];
125
124
return bucketLinkedList .search (element ) != -1 ;
126
125
}
127
126
@@ -137,14 +136,14 @@ public boolean contains(T element) {
137
136
* @return true if this set contained the specified element
138
137
*/
139
138
public boolean remove (T element ) {
140
- int bucket = this . hashFunction (element );
141
- LinkedList <T > bucketLinkedList = this . buckets [bucket ];
139
+ int bucket = hashFunction (element );
140
+ LinkedList <T > bucketLinkedList = buckets [bucket ];
142
141
int index = bucketLinkedList .search (element );
143
142
if (index == -1 ) {
144
143
return false ; // If the element is not in the hashset.
145
144
}
146
145
bucketLinkedList .remove (index );
147
- --this . size ; // updates the cardinality of the hash set.
146
+ --size ; // updates the cardinality of the hash set.
148
147
return true ;
149
148
}
150
149
@@ -155,7 +154,7 @@ public boolean remove(T element) {
155
154
*/
156
155
public List <T > toList () {
157
156
List <T > outputList = new ArrayList <>();
158
- for (LinkedList <T > bucket : this . buckets ) {
157
+ for (LinkedList <T > bucket : buckets ) {
159
158
while (bucket .size () != 0 ) {
160
159
outputList .add (bucket .pop ());
161
160
}
0 commit comments