File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change 1
1
package Sets ;
2
2
3
3
import java .util .*;
4
+ import java .util .function .Consumer ;
5
+
4
6
import Maps .HashMapCustom ;
5
7
6
- public class HashSetCustom <E > {
8
+ public class HashSetCustom <E > implements Iterable < E > {
7
9
8
10
public HashSetCustom () {
9
11
}
@@ -36,4 +38,29 @@ public String toString() {
36
38
return Arrays .toString (map .toKeyArray ());
37
39
}
38
40
41
+ public void forEach (Consumer <? super E > o ) {
42
+ map .forEach ((k , v ) -> {
43
+ o .accept (k );
44
+ });
45
+ }
46
+
47
+ @ Override
48
+ public Iterator <E > iterator () {
49
+ return new HashSetItr ();
50
+ }
51
+
52
+ private class HashSetItr implements Iterator <E > {
53
+ private Object [] arr = map .toKeyArray ();
54
+ int cursor = 0 ;
55
+
56
+ @ Override
57
+ public boolean hasNext () {
58
+ return cursor < arr .length ;
59
+ }
60
+
61
+ @ Override
62
+ public E next () {
63
+ return (E ) arr [cursor ++];
64
+ }
65
+ }
39
66
}
You can’t perform that action at this time.
0 commit comments