Skip to content

Commit 7248318

Browse files
committed
CustomHashSet now implements Set instead of Iterable
1 parent e48836d commit 7248318

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

src/main/java/by/andd3dfx/collections/custom/CustomHashMap.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public boolean isEmpty() {
2626
return size == 0;
2727
}
2828

29-
public V get(K key) {
29+
public V get(Object key) {
3030
if (key == null) {
3131
return valueForNullKey;
3232
}
@@ -71,7 +71,7 @@ public V put(K key, V value) {
7171
return null;
7272
}
7373

74-
public boolean containsKey(K key) {
74+
public boolean containsKey(Object key) {
7575
return get(key) != null;
7676
}
7777

@@ -86,7 +86,7 @@ public boolean containsValue(V value) {
8686
return false;
8787
}
8888

89-
public V remove(K key) {
89+
public V remove(Object key) {
9090
if (key == null) {
9191
var result = valueForNullKey;
9292
valueForNullKey = null;
@@ -122,7 +122,7 @@ Iterator<K> keyIterator() {
122122
return new KeyIterator<>(buckets);
123123
}
124124

125-
private int determineBucketNumber(K key) {
125+
private int determineBucketNumber(Object key) {
126126
return Math.abs(key.hashCode() % BUCKETS_COUNT);
127127
}
128128

src/main/java/by/andd3dfx/collections/custom/CustomHashSet.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
package by.andd3dfx.collections.custom;
22

3+
import org.apache.commons.lang3.NotImplementedException;
4+
35
import java.util.Collection;
46
import java.util.Iterator;
7+
import java.util.Set;
58

69
/**
710
* @see <a href="https://youtu.be/aTbKxApYNYk">Video solution</a>
811
*/
9-
public class CustomHashSet<T> implements Iterable<T> {
12+
public class CustomHashSet<T> implements Set<T> {
1013

11-
private CustomHashMap<T, Object> map = new CustomHashMap<>();
14+
private final CustomHashMap<T, Object> map = new CustomHashMap<>();
1215
private static final Object DUMMY_VALUE = new Object();
1316

17+
@Override
1418
public int size() {
1519
return map.size();
1620
}
1721

22+
@Override
1823
public boolean isEmpty() {
1924
return map.isEmpty();
2025
}
2126

27+
@Override
2228
public boolean add(T item) {
2329
return map.put(item, DUMMY_VALUE) == null;
2430
}
2531

26-
public boolean addAll(Collection<T> items) {
32+
@Override
33+
public boolean retainAll(Collection<?> c) {
34+
throw new UnsupportedOperationException();
35+
}
36+
37+
@Override
38+
public boolean addAll(Collection<? extends T> items) {
2739
boolean result = false;
2840
for (var item : items) {
2941
if (add(item)) {
@@ -33,11 +45,13 @@ public boolean addAll(Collection<T> items) {
3345
return result;
3446
}
3547

36-
public boolean remove(T item) {
48+
@Override
49+
public boolean remove(Object item) {
3750
return map.remove(item) == DUMMY_VALUE;
3851
}
3952

40-
public boolean removeAll(Collection<T> items) {
53+
@Override
54+
public boolean removeAll(Collection<?> items) {
4155
boolean result = false;
4256
for (var item : items) {
4357
if (remove(item)) {
@@ -47,11 +61,13 @@ public boolean removeAll(Collection<T> items) {
4761
return result;
4862
}
4963

50-
public boolean contains(T item) {
64+
@Override
65+
public boolean contains(Object item) {
5166
return map.containsKey(item);
5267
}
5368

54-
public boolean containsAll(Collection<T> items) {
69+
@Override
70+
public boolean containsAll(Collection<?> items) {
5571
for (var item : items) {
5672
if (!map.containsKey(item)) {
5773
return false;
@@ -60,6 +76,7 @@ public boolean containsAll(Collection<T> items) {
6076
return true;
6177
}
6278

79+
@Override
6380
public void clear() {
6481
map.clear();
6582
}
@@ -69,13 +86,25 @@ public Iterator<T> iterator() {
6986
return map.keyIterator();
7087
}
7188

89+
@Override
90+
public Object[] toArray() {
91+
// TODO add implementation
92+
throw new NotImplementedException();
93+
}
94+
95+
@Override
96+
public <T1> T1[] toArray(T1[] a) {
97+
// TODO add implementation
98+
throw new NotImplementedException();
99+
}
100+
72101
@Override
73102
public String toString() {
74103
StringBuilder sb = new StringBuilder();
75104
var it = iterator();
76105
while (it.hasNext()) {
77106
final T item = it.next();
78-
sb.append(item + ", ");
107+
sb.append(item).append(", ");
79108
}
80109
return "[" + sb.substring(0, sb.length() - 2) + "]";
81110
}

src/main/java/by/andd3dfx/collections/custom/CustomLinkedHashSet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public boolean add(T item) {
1919
}
2020

2121
@Override
22-
public boolean remove(T item) {
22+
public boolean remove(Object item) {
2323
final boolean result = super.remove(item);
2424
if (result) {
25-
linkedList.remove(item);
25+
linkedList.remove((T) item);
2626
}
2727
return result;
2828
}

0 commit comments

Comments
 (0)