Skip to content

Commit d14cc42

Browse files
shouvikbhattacharyamaibin
authored andcommitted
BAEL-1438: TreeMap vs HashMap (eugenp#3265)
* BAEL-1438: Final code. * BAEL-1438: Temporary commit. * BAEL-1438: New Changes. * BAEL-1438: New test case to support Java 8 features.
1 parent 2bff7a6 commit d14cc42

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.collection;
2+
3+
import java.util.ConcurrentModificationException;
4+
import java.util.HashMap;
5+
import java.util.Iterator;
6+
import java.util.Map;
7+
import java.util.TreeMap;
8+
9+
import org.hamcrest.Matchers;
10+
import org.junit.Assert;
11+
import org.junit.Test;
12+
13+
public class WhenComparingTreeMapVsHashMap {
14+
15+
@Test
16+
public void whenInsertObjectsTreeMap_thenNaturalOrder() {
17+
Map<Integer, String> treemap = new TreeMap<>();
18+
treemap.put(3, "TreeMap");
19+
treemap.put(2, "vs");
20+
treemap.put(1, "HashMap");
21+
Assert.assertThat(treemap.keySet(), Matchers.contains(1, 2, 3));
22+
}
23+
24+
@Test(expected = NullPointerException.class)
25+
public void whenInsertNullInTreeMap_thenException() {
26+
Map<Integer, String> treemap = new TreeMap<>();
27+
treemap.put(null, "NullPointerException");
28+
}
29+
30+
@Test
31+
public void whenInsertObjectsHashMap_thenRandomOrder() {
32+
Map<Integer, String> hashmap = new HashMap<>();
33+
hashmap.put(3, "TreeMap");
34+
hashmap.put(2, "vs");
35+
hashmap.put(1, "HashMap");
36+
Assert.assertThat(hashmap.keySet(), Matchers.containsInAnyOrder(1, 2, 3));
37+
}
38+
39+
@Test
40+
public void whenInsertNullInHashMap_thenInsertsNull() {
41+
Map<Integer, String> hashmap = new HashMap<>();
42+
hashmap.put(null, null);
43+
Assert.assertNull(hashmap.get(null));
44+
}
45+
46+
@Test
47+
public void givenHashMapAndTreeMap_whenputDuplicates_thenOnlyUnique() {
48+
Map<Integer, String> treeMap = new HashMap<>();
49+
treeMap.put(1, "Baeldung");
50+
treeMap.put(1, "Baeldung");
51+
52+
Assert.assertTrue(treeMap.size() == 1);
53+
54+
Map<Integer, String> treeMap2 = new TreeMap<>();
55+
treeMap2.put(1, "Baeldung");
56+
treeMap2.put(1, "Baeldung");
57+
58+
Assert.assertTrue(treeMap2.size() == 1);
59+
}
60+
}

‎testing-modules/junit-5/src/test/java/com/baeldung/ExceptionUnitTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6+
import java.util.ConcurrentModificationException;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
610
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.function.Executable;
712

813
public class ExceptionUnitTest {
914

@@ -22,4 +27,14 @@ void assertThrowsException() {
2227
Integer.valueOf(str);
2328
});
2429
}
30+
31+
@Test
32+
public void whenModifyMapDuringIteration_thenThrowExecption() {
33+
Map<Integer, String> hashmap = new HashMap<>();
34+
hashmap.put(1, "One");
35+
hashmap.put(2, "Two");
36+
37+
Executable executable = () -> hashmap.forEach((key, value) -> hashmap.remove(1));
38+
assertThrows(ConcurrentModificationException.class, executable);
39+
}
2540
}

0 commit comments

Comments
 (0)