Skip to content

Commit e1d5213

Browse files
authored
BAEL-8986 A Guide to HashSet in Java (#18081)
* Update WhenUsingHashSet.java * Create Employee.java * Update pom.xml * Update WhenUsingHashSet.java * Update Employee.java * Update WhenUsingHashSet.java
1 parent b9b2e73 commit e1d5213

File tree

3 files changed

+98
-14
lines changed

3 files changed

+98
-14
lines changed

core-java-modules/core-java-collections-set/pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
</parent>
1515

1616
<dependencies>
17+
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
18+
<dependency>
19+
<groupId>org.junit.jupiter</groupId>
20+
<artifactId>junit-jupiter</artifactId>
21+
<version>5.11.3</version>
22+
<scope>test</scope>
23+
</dependency>
1724
<dependency>
1825
<groupId>org.apache.commons</groupId>
1926
<artifactId>commons-collections4</artifactId>
@@ -44,4 +51,4 @@
4451
<gson.version>2.11.0</gson.version>
4552
</properties>
4653

47-
</project>
54+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.collection;
2+
3+
public class Employee implements Comparable<Employee> {
4+
private int employeeId;
5+
private String employeeName;
6+
7+
Employee(int employeeId, String employeeName) {
8+
this.employeeId = employeeId;
9+
this.employeeName = employeeName;
10+
}
11+
12+
int getEmployeeId() {
13+
return employeeId;
14+
}
15+
16+
public String getEmployeeName() {
17+
return employeeName;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return employeeId + " " + employeeName;
23+
}
24+
25+
@Override
26+
public int compareTo(Employee o) {
27+
if (this.employeeId == o.employeeId) {
28+
return 0;
29+
} else if (this.employeeId < o.employeeId) {
30+
return 1;
31+
} else {
32+
return -1;
33+
}
34+
}
35+
}

core-java-modules/core-java-collections-set/src/test/java/com/baeldung/collection/WhenUsingHashSet.java

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,62 @@
11
package com.baeldung.collection;
22

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+
39
import java.util.ConcurrentModificationException;
410
import java.util.HashSet;
511
import java.util.Iterator;
612
import java.util.Set;
7-
8-
import org.junit.Assert;
9-
import org.junit.Test;
13+
import java.util.TreeSet;
14+
1015

1116
public class WhenUsingHashSet {
1217

18+
private static HashSet<Employee> hashSet;
19+
private static TreeSet<Employee> treeSet;
20+
1321
@Test
1422
public void whenAddingElement_shouldAddElement() {
1523
Set<String> hashset = new HashSet<>();
16-
Assert.assertTrue(hashset.add("String Added"));
24+
assertTrue(hashset.add("String Added"));
1725
}
1826

1927
@Test
2028
public void whenCheckingForElement_shouldSearchForElement() {
2129
Set<String> hashsetContains = new HashSet<>();
2230
hashsetContains.add("String Added");
23-
Assert.assertTrue(hashsetContains.contains("String Added"));
31+
assertTrue(hashsetContains.contains("String Added"));
2432
}
2533

2634
@Test
2735
public void whenCheckingTheSizeOfHashSet_shouldReturnThesize() {
2836
Set<String> hashSetSize = new HashSet<>();
2937
hashSetSize.add("String Added");
30-
Assert.assertEquals(1, hashSetSize.size());
38+
assertEquals(1, hashSetSize.size());
3139
}
3240

3341
@Test
3442
public void whenCheckingForEmptyHashSet_shouldCheckForEmpty() {
3543
Set<String> emptyHashSet = new HashSet<>();
36-
Assert.assertTrue(emptyHashSet.isEmpty());
44+
assertTrue(emptyHashSet.isEmpty());
3745
}
3846

3947
@Test
4048
public void whenRemovingElement_shouldRemoveElement() {
4149
Set<String> removeFromHashSet = new HashSet<>();
4250
removeFromHashSet.add("String Added");
43-
Assert.assertTrue(removeFromHashSet.remove("String Added"));
51+
assertTrue(removeFromHashSet.remove("String Added"));
4452
}
4553

4654
@Test
4755
public void whenClearingHashSet_shouldClearHashSet() {
4856
Set<String> clearHashSet = new HashSet<>();
4957
clearHashSet.add("String Added");
5058
clearHashSet.clear();
51-
Assert.assertTrue(clearHashSet.isEmpty());
59+
assertTrue(clearHashSet.isEmpty());
5260
}
5361

5462
@Test
@@ -63,17 +71,19 @@ public void whenIteratingHashSet_shouldIterateHashSet() {
6371
}
6472
}
6573

66-
@Test(expected = ConcurrentModificationException.class)
74+
@Test
6775
public void whenModifyingHashSetWhileIterating_shouldThrowException() {
6876
Set<String> hashset = new HashSet<>();
6977
hashset.add("First");
7078
hashset.add("Second");
7179
hashset.add("Third");
7280
Iterator<String> itr = hashset.iterator();
73-
while (itr.hasNext()) {
81+
assertThrows(ConcurrentModificationException.class,() -> {
82+
while (itr.hasNext()) {
7483
itr.next();
7584
hashset.remove("Second");
76-
}
85+
}
86+
});
7787
}
7888

7989
@Test
@@ -88,6 +98,38 @@ public void whenRemovingElementUsingIterator_shouldRemoveElement() {
8898
if (element.equals("Second"))
8999
itr.remove();
90100
}
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+
});
92134
}
93135
}

0 commit comments

Comments
 (0)