From d14cc423068d9799381815a012d4caec23c5d11e Mon Sep 17 00:00:00 2001 From: Shouvik Bhattacharya <33756821+shouvikbhattacharya@users.noreply.github.com> Date: Thu, 21 Dec 2017 22:14:27 +0530 Subject: [PATCH] BAEL-1438: TreeMap vs HashMap (#3265) * BAEL-1438: Final code. * BAEL-1438: Temporary commit. * BAEL-1438: New Changes. * BAEL-1438: New test case to support Java 8 features. --- .../WhenComparingTreeMapVsHashMap.java | 60 +++++++++++++++++++ .../java/com/baeldung/ExceptionUnitTest.java | 15 +++++ 2 files changed, 75 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/collection/WhenComparingTreeMapVsHashMap.java diff --git a/core-java/src/test/java/com/baeldung/collection/WhenComparingTreeMapVsHashMap.java b/core-java/src/test/java/com/baeldung/collection/WhenComparingTreeMapVsHashMap.java new file mode 100644 index 0000000000..f2dfc992c2 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/collection/WhenComparingTreeMapVsHashMap.java @@ -0,0 +1,60 @@ +package com.baeldung.collection; + +import java.util.ConcurrentModificationException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.TreeMap; + +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Test; + +public class WhenComparingTreeMapVsHashMap { + + @Test + public void whenInsertObjectsTreeMap_thenNaturalOrder() { + Map treemap = new TreeMap<>(); + treemap.put(3, "TreeMap"); + treemap.put(2, "vs"); + treemap.put(1, "HashMap"); + Assert.assertThat(treemap.keySet(), Matchers.contains(1, 2, 3)); + } + + @Test(expected = NullPointerException.class) + public void whenInsertNullInTreeMap_thenException() { + Map treemap = new TreeMap<>(); + treemap.put(null, "NullPointerException"); + } + + @Test + public void whenInsertObjectsHashMap_thenRandomOrder() { + Map hashmap = new HashMap<>(); + hashmap.put(3, "TreeMap"); + hashmap.put(2, "vs"); + hashmap.put(1, "HashMap"); + Assert.assertThat(hashmap.keySet(), Matchers.containsInAnyOrder(1, 2, 3)); + } + + @Test + public void whenInsertNullInHashMap_thenInsertsNull() { + Map hashmap = new HashMap<>(); + hashmap.put(null, null); + Assert.assertNull(hashmap.get(null)); + } + + @Test + public void givenHashMapAndTreeMap_whenputDuplicates_thenOnlyUnique() { + Map treeMap = new HashMap<>(); + treeMap.put(1, "Baeldung"); + treeMap.put(1, "Baeldung"); + + Assert.assertTrue(treeMap.size() == 1); + + Map treeMap2 = new TreeMap<>(); + treeMap2.put(1, "Baeldung"); + treeMap2.put(1, "Baeldung"); + + Assert.assertTrue(treeMap2.size() == 1); + } +} \ No newline at end of file diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/ExceptionUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/ExceptionUnitTest.java index bd57f5b3cb..440c3e40ab 100644 --- a/testing-modules/junit-5/src/test/java/com/baeldung/ExceptionUnitTest.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/ExceptionUnitTest.java @@ -3,7 +3,12 @@ package com.baeldung; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.util.ConcurrentModificationException; +import java.util.HashMap; +import java.util.Map; + import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; public class ExceptionUnitTest { @@ -22,4 +27,14 @@ public class ExceptionUnitTest { Integer.valueOf(str); }); } + + @Test + public void whenModifyMapDuringIteration_thenThrowExecption() { + Map hashmap = new HashMap<>(); + hashmap.put(1, "One"); + hashmap.put(2, "Two"); + + Executable executable = () -> hashmap.forEach((key, value) -> hashmap.remove(1)); + assertThrows(ConcurrentModificationException.class, executable); + } }