11package by .andd3dfx .collections .custom ;
22
3+ import org .apache .commons .lang3 .NotImplementedException ;
4+
35import java .util .Collection ;
46import 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 }
0 commit comments