Skip to content

Commit 23fe71d

Browse files
committed
Revert "UNFINISHED: implement Collection by CustomHashSet"
This reverts commit 3f5ca19.
1 parent 421c70f commit 23fe71d

File tree

6 files changed

+20
-208
lines changed

6 files changed

+20
-208
lines changed

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

Lines changed: 9 additions & 43 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(Object key) {
29+
public V get(K key) {
3030
if (key == null) {
3131
return valueForNullKey;
3232
}
@@ -53,7 +53,7 @@ public V put(K key, V value) {
5353

5454
int bucketNumber = determineBucketNumber(key);
5555
if (buckets[bucketNumber].isEmpty()) {
56-
buckets[bucketNumber].add(new CustomEntry<>(key, value));
56+
buckets[bucketNumber].add(new CustomEntry(key, value));
5757
size++;
5858
return null;
5959
}
@@ -66,12 +66,12 @@ public V put(K key, V value) {
6666
}
6767
}
6868

69-
buckets[bucketNumber].add(new CustomEntry<>(key, value));
69+
buckets[bucketNumber].add(new CustomEntry(key, value));
7070
size++;
7171
return null;
7272
}
7373

74-
public boolean containsKey(Object key) {
74+
public boolean containsKey(K 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(Object key) {
89+
public V remove(K key) {
9090
if (key == null) {
9191
var result = valueForNullKey;
9292
valueForNullKey = null;
@@ -95,19 +95,13 @@ public V remove(Object key) {
9595
}
9696

9797
int bucketNumber = determineBucketNumber(key);
98-
var bucket = buckets[bucketNumber];
99-
var iterator = bucket.iterator();
100-
int index = 0;
101-
102-
while (iterator.hasNext()) {
103-
var curr = iterator.next();
98+
for (var curr : buckets[bucketNumber]) {
10499
if (checkEquality(key, curr.getKey())) {
105100
final V result = curr.getValue();
106-
bucket.remove(index);
101+
buckets[bucketNumber].remove(curr);
107102
size--;
108103
return result;
109104
}
110-
index++;
111105
}
112106
return null;
113107
}
@@ -128,16 +122,14 @@ Iterator<K> keyIterator() {
128122
return new KeyIterator<>(buckets);
129123
}
130124

131-
private int determineBucketNumber(Object key) {
125+
private int determineBucketNumber(K key) {
132126
return Math.abs(key.hashCode() % BUCKETS_COUNT);
133127
}
134128

135129
private class KeyIterator<E> implements Iterator<E> {
136130
private final CustomLinkedList<CustomEntry<E, V>>[] buckets;
137131
private int currentBucketIndex;
138132
private Iterator<CustomEntry<E, V>> currentIterator;
139-
private CustomEntry<E, V> lastReturned;
140-
private boolean canRemove = false;
141133

142134
public KeyIterator(CustomLinkedList<CustomEntry<E, V>>[] buckets) {
143135
this.buckets = buckets;
@@ -161,9 +153,7 @@ public boolean hasNext() {
161153
@Override
162154
public E next() {
163155
if (currentIterator.hasNext()) {
164-
lastReturned = currentIterator.next();
165-
canRemove = true;
166-
return lastReturned.getKey();
156+
return currentIterator.next().getKey();
167157
}
168158
if (currentBucketIndex < buckets.length - 1) {
169159
currentBucketIndex++;
@@ -172,29 +162,6 @@ public E next() {
172162
}
173163
throw new NoSuchElementException();
174164
}
175-
176-
@Override
177-
public void remove() {
178-
if (!canRemove) {
179-
throw new IllegalStateException("remove() can only be called after next()");
180-
}
181-
if (lastReturned != null) {
182-
// Находим индекс элемента в текущем bucket и удаляем его
183-
var bucket = buckets[currentBucketIndex];
184-
var iterator = bucket.iterator();
185-
int index = 0;
186-
while (iterator.hasNext()) {
187-
var curr = iterator.next();
188-
if (curr == lastReturned) {
189-
bucket.remove(index);
190-
size--;
191-
canRemove = false;
192-
return;
193-
}
194-
index++;
195-
}
196-
}
197-
}
198165
}
199166

200167
@Override
@@ -215,7 +182,6 @@ private boolean checkEquality(Object o1, Object o2) {
215182
return o1.equals(o2);
216183
}
217184

218-
@SuppressWarnings("unchecked")
219185
private void initialize() {
220186
buckets = new CustomLinkedList[BUCKETS_COUNT];
221187
for (var i = 0; i < BUCKETS_COUNT; i++) {

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

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

3-
import java.util.Arrays;
43
import java.util.Collection;
54
import java.util.Iterator;
6-
import java.util.stream.Stream;
75

86
/**
97
* @see <a href="https://youtu.be/aTbKxApYNYk">Video solution</a>
108
*/
11-
public class CustomHashSet<T> implements Collection<T> {
9+
public class CustomHashSet<T> implements Iterable<T> {
1210

1311
private CustomHashMap<T, Object> map = new CustomHashMap<>();
1412
private static final Object DUMMY_VALUE = new Object();
1513

16-
@Override
1714
public int size() {
1815
return map.size();
1916
}
2017

21-
@Override
2218
public boolean isEmpty() {
2319
return map.isEmpty();
2420
}
2521

26-
@Override
2722
public boolean add(T item) {
2823
return map.put(item, DUMMY_VALUE) == null;
2924
}
3025

31-
@Override
32-
public boolean addAll(Collection<? extends T> items) {
26+
public boolean addAll(Collection<T> items) {
3327
boolean result = false;
3428
for (var item : items) {
3529
if (add(item)) {
@@ -39,13 +33,11 @@ public boolean addAll(Collection<? extends T> items) {
3933
return result;
4034
}
4135

42-
@Override
43-
public boolean remove(Object item) {
36+
public boolean remove(T item) {
4437
return map.remove(item) == DUMMY_VALUE;
4538
}
4639

47-
@Override
48-
public boolean removeAll(Collection<?> items) {
40+
public boolean removeAll(Collection<T> items) {
4941
boolean result = false;
5042
for (var item : items) {
5143
if (remove(item)) {
@@ -55,28 +47,11 @@ public boolean removeAll(Collection<?> items) {
5547
return result;
5648
}
5749

58-
@Override
59-
public boolean retainAll(Collection<?> c) {
60-
var modified = false;
61-
final Iterator<T> iterator = iterator();
62-
63-
while (iterator.hasNext()) {
64-
final T item = iterator.next();
65-
if (!c.contains(item)) {
66-
iterator.remove();
67-
modified = true;
68-
}
69-
}
70-
return modified;
71-
}
72-
73-
@Override
74-
public boolean contains(Object item) {
50+
public boolean contains(T item) {
7551
return map.containsKey(item);
7652
}
7753

78-
@Override
79-
public boolean containsAll(Collection<?> items) {
54+
public boolean containsAll(Collection<T> items) {
8055
for (var item : items) {
8156
if (!map.containsKey(item)) {
8257
return false;
@@ -85,7 +60,6 @@ public boolean containsAll(Collection<?> items) {
8560
return true;
8661
}
8762

88-
@Override
8963
public void clear() {
9064
map.clear();
9165
}
@@ -95,26 +69,6 @@ public Iterator<T> iterator() {
9569
return map.keyIterator();
9670
}
9771

98-
@Override
99-
public Object[] toArray() {
100-
var list = new CustomArrayList<>();
101-
var iterator = map.keyIterator();
102-
while (iterator.hasNext()) {
103-
list.add(iterator.next());
104-
}
105-
return list.toArray();
106-
}
107-
108-
@Override
109-
public <T1> T1[] toArray(T1[] a) {
110-
var src = toArray();
111-
var result = (T1[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size());
112-
113-
System.arraycopy(src, 0, result, 0, size());
114-
115-
return result;
116-
}
117-
11872
@Override
11973
public String toString() {
12074
StringBuilder sb = new StringBuilder();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public boolean add(T item) {
1919
}
2020

2121
@Override
22-
public boolean remove(Object item) {
22+
public boolean remove(T item) {
2323
final boolean result = super.remove(item);
2424
if (result) {
2525
linkedList.remove(item);

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

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public T remove(int index) {
120120
return curr.value;
121121
}
122122

123-
public boolean remove(Object value) {
123+
public boolean remove(T value) {
124124
var i = 0;
125125
var curr = head;
126126
while (curr != null) {
@@ -134,21 +134,7 @@ public boolean remove(Object value) {
134134
return false;
135135
}
136136

137-
private boolean removeNode(Node<T> nodeToDelete) {
138-
var i = 0;
139-
var curr = head;
140-
while (curr != null) {
141-
if (checkEquality(nodeToDelete, curr)) {
142-
remove(i);
143-
return true;
144-
}
145-
i++;
146-
curr = curr.next;
147-
}
148-
return false;
149-
}
150-
151-
private boolean checkEquality(Object value1, Object value2) {
137+
private boolean checkEquality(T value1, T value2) {
152138
if (value1 == null) {
153139
return value2 == null;
154140
}
@@ -183,20 +169,12 @@ public void clear() {
183169

184170
@Override
185171
public Iterator<T> iterator() {
186-
return new CustomIterator<>(this);
172+
return new CustomIterator<>(head);
187173
}
188174

175+
@AllArgsConstructor
189176
public static class CustomIterator<E> implements Iterator<E> {
190-
private final CustomLinkedList<E> list;
191177
private Node<E> curr;
192-
private Node<E> prevReturned;
193-
private boolean canRemove = false;
194-
195-
public CustomIterator(CustomLinkedList<E> list) {
196-
this.list = list;
197-
this.curr = list.head;
198-
this.prevReturned = null;
199-
}
200178

201179
@Override
202180
public boolean hasNext() {
@@ -205,26 +183,10 @@ public boolean hasNext() {
205183

206184
@Override
207185
public E next() {
208-
if (curr == null) {
209-
throw new IllegalStateException("No more elements");
210-
}
211186
var result = curr.value;
212-
prevReturned = curr;
213187
curr = curr.next;
214-
canRemove = true;
215188
return result;
216189
}
217-
218-
@Override
219-
public void remove() {
220-
if (!canRemove) {
221-
throw new IllegalStateException("remove() can only be called after next()");
222-
}
223-
if (prevReturned != null) {
224-
list.removeNode(prevReturned);
225-
canRemove = false;
226-
}
227-
}
228190
}
229191

230192
@Override

0 commit comments

Comments
 (0)