File tree 2 files changed +97
-0
lines changed
main/java/br/com/zevolution/datastructure/set
test/java/br/com/zevolution/datastructure/set
2 files changed +97
-0
lines changed Original file line number Diff line number Diff line change
1
+ package br .com .zevolution .datastructure .set ;
2
+
3
+ import java .util .ArrayList ;
4
+
5
+ import br .com .zevolution .datastructure .doublylinkedlist .DoublyLinkedList ;
6
+
7
+ public class CustomSet {
8
+
9
+ private ArrayList <DoublyLinkedList > set = new ArrayList <>(26 );
10
+ private int totalElements = 0 ;
11
+
12
+ public CustomSet () {
13
+ for (int i = 0 ; i < 26 ; i ++) {
14
+ this .set .add (new DoublyLinkedList ());
15
+ }
16
+ }
17
+
18
+ public void add (String word ) {
19
+ if (!this .has (word )) {
20
+ this .set .get (this .getHashCode (word )).add (word );
21
+ this .totalElements ++;
22
+ }
23
+ }
24
+
25
+ public boolean has (String word ) {
26
+ return this .set .get (this .getHashCode (word )).has (word );
27
+ }
28
+
29
+ public void remove (String word ) {
30
+ if (this .has (word )) {
31
+ this .set .get (this .getHashCode (word )).remove (word );
32
+ this .totalElements --;
33
+ }
34
+ }
35
+
36
+ public int size () {
37
+ return this .totalElements ;
38
+ }
39
+
40
+ @ Override
41
+ public String toString () {
42
+ return this .set .toString ();
43
+ }
44
+
45
+ private int getHashCode (String word ) {
46
+ if (word != null ) {
47
+ return word .toLowerCase ().charAt (0 ) % 26 ;
48
+ } else {
49
+ throw new NullPointerException ();
50
+ }
51
+ }
52
+ }
Original file line number Diff line number Diff line change
1
+ package br .com .zevolution .datastructure .set ;
2
+
3
+ import static org .junit .Assert .assertEquals ;
4
+
5
+ import org .junit .Test ;
6
+
7
+ public class CustomSetTest {
8
+
9
+ @ Test
10
+ public void should_Contain_OneRecordInCustomSet () {
11
+ CustomSet set = new CustomSet ();
12
+ set .add ("José Lucas" );
13
+
14
+ assertEquals (1 , set .size ());
15
+ }
16
+
17
+ @ Test
18
+ public void should_Be_Empty () {
19
+ CustomSet set = new CustomSet ();
20
+ set .add ("José Lucas" );
21
+
22
+ set .remove ("José Lucas" );
23
+
24
+ assertEquals (0 , set .size ());
25
+ }
26
+
27
+ @ Test
28
+ public void should_NotBe_Duplicated () {
29
+ CustomSet set = new CustomSet ();
30
+ set .add ("José Lucas" );
31
+ set .add ("José Lucas" );
32
+ set .add ("José Lucas" );
33
+
34
+ assertEquals (1 , set .size ());
35
+ }
36
+
37
+ @ Test
38
+ public void should_Has_Word () {
39
+ CustomSet set = new CustomSet ();
40
+ set .add ("José Lucas" );
41
+
42
+ assertEquals (true , set .has ("José Lucas" ));
43
+ }
44
+
45
+ }
You can’t perform that action at this time.
0 commit comments