diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/Main.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/Main.java new file mode 100644 index 0000000000..1024399a98 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/Main.java @@ -0,0 +1,25 @@ +package com.baeldung.mapandhashmap; + +import com.baeldung.mapandhashmap.printer.HashMapPrinter; +import com.baeldung.mapandhashmap.printer.MapPrinter; +import java.util.HashMap; +import java.util.Map; +import java.util.TreeMap; + +public class Main { + public static void main(String[] args) { + Map map = new HashMap<>(); + HashMap hashMap = new HashMap<>(); + TreeMap treeMap = new TreeMap<>(); + + HashMapPrinter hashMapPrinter = new HashMapPrinter(); + hashMapPrinter.printMap(hashMap); +// hashMapPrinter.printMap(treeMap); Compile time error +// hashMapPrinter.printMap(map); Compile time error + + MapPrinter mapPrinter = new MapPrinter(); + mapPrinter.printMap(hashMap); + mapPrinter.printMap(treeMap); + mapPrinter.printMap(map); + } +} diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/HashMapPrinter.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/HashMapPrinter.java new file mode 100644 index 0000000000..53c78bfc55 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/HashMapPrinter.java @@ -0,0 +1,13 @@ +package com.baeldung.mapandhashmap.printer; + +import java.util.HashMap; +import java.util.Map.Entry; + +public class HashMapPrinter { + + public void printMap(final HashMap map) { + for (final Entry entry : map.entrySet()) { + System.out.println(entry.getKey() + " " + entry.getValue()); + } + } +} diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/MapPrinter.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/MapPrinter.java new file mode 100644 index 0000000000..e5c0ab49cd --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/MapPrinter.java @@ -0,0 +1,13 @@ +package com.baeldung.mapandhashmap.printer; + +import java.util.Map; +import java.util.Map.Entry; + +public class MapPrinter { + + public void printMap(final Map map) { + for (final Entry entry : map.entrySet()) { + System.out.println(entry.getKey() + " " + entry.getValue()); + } + } +} diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/MapReporter.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/MapReporter.java new file mode 100644 index 0000000000..fd7347c2d1 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/mapandhashmap/printer/MapReporter.java @@ -0,0 +1,19 @@ +package com.baeldung.mapandhashmap.printer; + +import java.util.Map; +import java.util.Map.Entry; + +public class MapReporter { + + private final Map map; + + public MapReporter(final Map map) { + this.map = map; + } + + public void printMap() { + for (final Entry entry : this.map.entrySet()) { + System.out.println(entry.getKey() + " " + entry.getValue()); + } + } +} diff --git a/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/HashMapPrinterUnitTest.java b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/HashMapPrinterUnitTest.java new file mode 100644 index 0000000000..e1147f6e00 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/HashMapPrinterUnitTest.java @@ -0,0 +1,56 @@ +package com.baeldung.mapandhashmap.printer; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.HashMap; +import java.util.LinkedHashMap; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class HashMapPrinterUnitTest { + + private final HashMapPrinter mapPrinter = new HashMapPrinter(); + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + + @BeforeEach + public void setUp() { + System.setOut(new PrintStream(outputStreamCaptor)); + } + + @Test + @DisplayName("Test hash map printer with HashMap") + void testPrintHashMap() { + // given + String key = "HashMap"; + String value = "Main default implementation for the Map interface"; + String expected = key + " " + value; + HashMap map = new HashMap<>(); + map.put(key, value); + // when + mapPrinter.printMap(map); + // then + String actual = outputStreamCaptor.toString().trim(); + assertThat(actual).isEqualTo(expected); + + } + + @Test + @DisplayName("Test hash map printer with LinkedHash") + void testPrintLinkedHashMap() { + // given + String key = "LinkedHashMap"; + String value = "Use this implementation if you need keep the order of elements"; + String expected = key + " " + value; + LinkedHashMap map = new LinkedHashMap<>(); + map.put(key, value); + // when + mapPrinter.printMap(map); + // then + String actual = outputStreamCaptor.toString().trim(); + assertThat(actual).isEqualTo(expected); + + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/MapPrinterUnitTest.java b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/MapPrinterUnitTest.java new file mode 100644 index 0000000000..8c45758bdf --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/MapPrinterUnitTest.java @@ -0,0 +1,71 @@ +package com.baeldung.mapandhashmap.printer; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.TreeMap; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class MapPrinterUnitTest { + + private final MapPrinter mapPrinter = new MapPrinter(); + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + + @BeforeEach + public void setUp() { + System.setOut(new PrintStream(outputStreamCaptor)); + } + + @Test + @DisplayName("Test printer with TreeMap") + void testPrintTreeMap() { + // given + String key = "TreeMap"; + String value = "Used when sorting is needed"; + String expected = key + " " + value; + TreeMap map = new TreeMap<>(); + map.put(key, value); + // when + mapPrinter.printMap(map); + // then + String actual = outputStreamCaptor.toString().trim(); + assertThat(actual).isEqualTo(expected); + } + + @Test + @DisplayName("Test printer with HashMap") + void testPrintHashMap() { + // given + String key = "HashMap"; + String value = "Main default implementation for the Map interface"; + String expected = key + " " + value; + HashMap map = new HashMap<>(); + map.put(key, value); + // when + mapPrinter.printMap(map); + // then + String actual = outputStreamCaptor.toString().trim(); + assertThat(actual).isEqualTo(expected); + } + + @Test + @DisplayName("Test printer with LinkedHash") + void testPrintLinkedHashMap() { + // given + String key = "LinkedHashMap"; + String value = "Use this implementation if you need keep the order of elements"; + String expected = key + " " + value; + LinkedHashMap map = new LinkedHashMap<>(); + map.put(key, value); + // when + mapPrinter.printMap(map); + // then + String actual = outputStreamCaptor.toString().trim(); + assertThat(actual).isEqualTo(expected); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/MapReporterUnitTest.java b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/MapReporterUnitTest.java new file mode 100644 index 0000000000..8f858a75c4 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/mapandhashmap/printer/MapReporterUnitTest.java @@ -0,0 +1,74 @@ +package com.baeldung.mapandhashmap.printer; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.TreeMap; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class MapReporterUnitTest { + + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + + @BeforeEach + public void setUp() { + System.setOut(new PrintStream(outputStreamCaptor)); + } + + @Test + @DisplayName("Test reporter with TreeMap") + void testPrintTreeMap() { + // given + String key = "TreeMap"; + String value = "Used when sorting is needed"; + String expected = key + " " + value; + TreeMap map = new TreeMap<>(); + map.put(key, value); + // when + MapReporter mapReporter = new MapReporter(map); + mapReporter.printMap(); + // then + String actual = outputStreamCaptor.toString().trim(); + assertThat(actual).isEqualTo(expected); + } + + @Test + @DisplayName("Test reporter with HashMap") + void testPrintHashMap() { + // given + String key = "HashMap"; + String value = "Main default implementation for the Map interface"; + String expected = key + " " + value; + HashMap map = new HashMap<>(); + map.put(key, value); + // when + MapReporter mapReporter = new MapReporter(map); + mapReporter.printMap(); + // then + String actual = outputStreamCaptor.toString().trim(); + assertThat(actual).isEqualTo(expected); + } + + @Test + @DisplayName("Test reporter with LinkedHash") + void testPrintLinkedHashMap() { + // given + String key = "LinkedHashMap"; + String value = "Use this implementation if you need keep the order of elements"; + String expected = key + " " + value; + LinkedHashMap map = new LinkedHashMap<>(); + map.put(key, value); + // when + MapReporter mapReporter = new MapReporter(map); + mapReporter.printMap(); + // then + String actual = outputStreamCaptor.toString().trim(); + assertThat(actual).isEqualTo(expected); + } + +} \ No newline at end of file