BAEL-5349 Example for the difference between map and hashmap (#11827)

* example for the difference between map and hashmap

* unit tests for the example

* rename unit tests

Co-authored-by: eugene.kovko <eugene.kovko@tensquaregames.com>
This commit is contained in:
Eugene Kovko 2022-02-22 02:05:08 +01:00 committed by GitHub
parent 082d48e291
commit 5ee332c606
7 changed files with 271 additions and 0 deletions

View File

@ -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<String, String> map = new HashMap<>();
HashMap<String, String> hashMap = new HashMap<>();
TreeMap<String, String> 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);
}
}

View File

@ -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());
}
}
}

View File

@ -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());
}
}
}

View File

@ -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());
}
}
}

View File

@ -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<String, String> 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<String, String> map = new LinkedHashMap<>();
map.put(key, value);
// when
mapPrinter.printMap(map);
// then
String actual = outputStreamCaptor.toString().trim();
assertThat(actual).isEqualTo(expected);
}
}

View File

@ -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<String, String> 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<String, String> 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<String, String> map = new LinkedHashMap<>();
map.put(key, value);
// when
mapPrinter.printMap(map);
// then
String actual = outputStreamCaptor.toString().trim();
assertThat(actual).isEqualTo(expected);
}
}

View File

@ -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<String, String> 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<String, String> 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<String, String> 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);
}
}