1
1
package com .baeldung .collection ;
2
2
3
+ import org .junit .jupiter .api .Test ;
4
+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
5
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
6
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
7
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
8
+
3
9
import java .util .ConcurrentModificationException ;
4
10
import java .util .HashSet ;
5
11
import java .util .Iterator ;
6
12
import java .util .Set ;
7
-
8
- import org .junit .Assert ;
9
- import org .junit .Test ;
13
+ import java .util .TreeSet ;
14
+
10
15
11
16
public class WhenUsingHashSet {
12
17
18
+ private static HashSet <Employee > hashSet ;
19
+ private static TreeSet <Employee > treeSet ;
20
+
13
21
@ Test
14
22
public void whenAddingElement_shouldAddElement () {
15
23
Set <String > hashset = new HashSet <>();
16
- Assert . assertTrue (hashset .add ("String Added" ));
24
+ assertTrue (hashset .add ("String Added" ));
17
25
}
18
26
19
27
@ Test
20
28
public void whenCheckingForElement_shouldSearchForElement () {
21
29
Set <String > hashsetContains = new HashSet <>();
22
30
hashsetContains .add ("String Added" );
23
- Assert . assertTrue (hashsetContains .contains ("String Added" ));
31
+ assertTrue (hashsetContains .contains ("String Added" ));
24
32
}
25
33
26
34
@ Test
27
35
public void whenCheckingTheSizeOfHashSet_shouldReturnThesize () {
28
36
Set <String > hashSetSize = new HashSet <>();
29
37
hashSetSize .add ("String Added" );
30
- Assert . assertEquals (1 , hashSetSize .size ());
38
+ assertEquals (1 , hashSetSize .size ());
31
39
}
32
40
33
41
@ Test
34
42
public void whenCheckingForEmptyHashSet_shouldCheckForEmpty () {
35
43
Set <String > emptyHashSet = new HashSet <>();
36
- Assert . assertTrue (emptyHashSet .isEmpty ());
44
+ assertTrue (emptyHashSet .isEmpty ());
37
45
}
38
46
39
47
@ Test
40
48
public void whenRemovingElement_shouldRemoveElement () {
41
49
Set <String > removeFromHashSet = new HashSet <>();
42
50
removeFromHashSet .add ("String Added" );
43
- Assert . assertTrue (removeFromHashSet .remove ("String Added" ));
51
+ assertTrue (removeFromHashSet .remove ("String Added" ));
44
52
}
45
53
46
54
@ Test
47
55
public void whenClearingHashSet_shouldClearHashSet () {
48
56
Set <String > clearHashSet = new HashSet <>();
49
57
clearHashSet .add ("String Added" );
50
58
clearHashSet .clear ();
51
- Assert . assertTrue (clearHashSet .isEmpty ());
59
+ assertTrue (clearHashSet .isEmpty ());
52
60
}
53
61
54
62
@ Test
@@ -63,17 +71,19 @@ public void whenIteratingHashSet_shouldIterateHashSet() {
63
71
}
64
72
}
65
73
66
- @ Test ( expected = ConcurrentModificationException . class )
74
+ @ Test
67
75
public void whenModifyingHashSetWhileIterating_shouldThrowException () {
68
76
Set <String > hashset = new HashSet <>();
69
77
hashset .add ("First" );
70
78
hashset .add ("Second" );
71
79
hashset .add ("Third" );
72
80
Iterator <String > itr = hashset .iterator ();
73
- while (itr .hasNext ()) {
81
+ assertThrows (ConcurrentModificationException .class ,() -> {
82
+ while (itr .hasNext ()) {
74
83
itr .next ();
75
84
hashset .remove ("Second" );
76
- }
85
+ }
86
+ });
77
87
}
78
88
79
89
@ Test
@@ -88,6 +98,38 @@ public void whenRemovingElementUsingIterator_shouldRemoveElement() {
88
98
if (element .equals ("Second" ))
89
99
itr .remove ();
90
100
}
91
- Assert .assertEquals (2 , hashset .size ());
101
+ assertEquals (2 , hashset .size ());
102
+ }
103
+
104
+ @ Test
105
+ public void givenNonComparableObject_whenConvertingToTreeSet_thenExceptionThrown () {
106
+
107
+ HashSet <Employee > hashSet = new HashSet <Employee >();
108
+
109
+ hashSet .add (new Employee (3 , "John" ));
110
+ hashSet .add (new Employee (5 , "Mike" ));
111
+ hashSet .add (new Employee (2 , "Bob" ));
112
+ hashSet .add (new Employee (1 , "Tom" ));
113
+ hashSet .add (new Employee (4 , "Johnny" ));
114
+
115
+ assertThrows (ClassCastException .class ,() -> {
116
+ TreeSet <Employee > treeSet = new TreeSet <Employee >(hashSet );
117
+ });
118
+ }
119
+
120
+ @ Test
121
+ public void givenComparableObject_whenConvertingToTreeSet_thenNoExceptionThrown () {
122
+
123
+ HashSet <Employee > hashSet = new HashSet <Employee >();
124
+
125
+ hashSet .add (new Employee (3 , "John" ));
126
+ hashSet .add (new Employee (5 , "Mike" ));
127
+ hashSet .add (new Employee (2 , "Bob" ));
128
+ hashSet .add (new Employee (1 , "Tom" ));
129
+ hashSet .add (new Employee (4 , "Johnny" ));
130
+
131
+ assertDoesNotThrow (()->{
132
+ TreeSet <Employee > treeSet =new TreeSet <Employee >(hashSet );
133
+ });
92
134
}
93
135
}
0 commit comments