From 3a2f05b17d17ccf866a02526777670a7c7c22bbb Mon Sep 17 00:00:00 2001 From: danielmcnally285 <144589379+danielmcnally285@users.noreply.github.com> Date: Wed, 14 Feb 2024 01:23:19 +0000 Subject: [PATCH] BAEL-6764: Collect stream of entrySet() to LinkedHashMap (#15806) * entryset to linkedhashmap unit tests * use set over list for groupingBy unit test * add clearer unit test method names * remove public modifier from unit tests * rename unit test as per pr comment --- .../EntrySetToLinkedHashMapUnitTest.java | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/entrysettolinkedhashmap/EntrySetToLinkedHashMapUnitTest.java diff --git a/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/entrysettolinkedhashmap/EntrySetToLinkedHashMapUnitTest.java b/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/entrysettolinkedhashmap/EntrySetToLinkedHashMapUnitTest.java new file mode 100644 index 0000000000..aa8398893e --- /dev/null +++ b/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/entrysettolinkedhashmap/EntrySetToLinkedHashMapUnitTest.java @@ -0,0 +1,111 @@ +package com.baeldung.map.entrysettolinkedhashmap; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class EntrySetToLinkedHashMapUnitTest { + + private Map map; + + @Test + void givenMap_whenUsingCollectorsGroupingBy_thenCollectToLinkedHashMap() { + Map> countryToCities = Map.of("Paris", "France", "Nice", "France", "Madrid", "Spain") + .entrySet() + .stream() + .collect(Collectors.groupingBy(Map.Entry::getValue, LinkedHashMap::new, Collectors.mapping(Map.Entry::getKey, Collectors.toSet()))); + + assertThat(countryToCities).isExactlyInstanceOf(LinkedHashMap.class) + .containsOnly(entry("France", Set.of("Paris", "Nice")), entry("Spain", Set.of("Madrid"))); + } + + @Test + void givenMap_whenUsingCollectorsToMap_thenCollectAndConvertToLinkedHashMap() { + Map result = new LinkedHashMap<>(map.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + + assertThat(result).isExactlyInstanceOf(LinkedHashMap.class) + .containsOnly(entry(1, "value 1"), entry(2, "value 2")); + } + + @Test + void givenMap_whenUsingCollectorsToMap_thenCollectToLinkedHashMap() { + Map result = map.entrySet() + .stream() + .collect(Collectors.toMap( + Map.Entry::getKey, Map.Entry::getValue, + (e1, e2) -> { + throw new RuntimeException(); + }, + LinkedHashMap::new)); + + assertThat(result).isExactlyInstanceOf(LinkedHashMap.class) + .containsOnly(entry(1, "value 1"), entry(2, "value 2")); + } + + @Test + void givenMap_whenUsingLinkedHashMapConstructor_thenObtainLinkedHashMap() { + Map result = new LinkedHashMap<>(map); + + assertThat(result).isExactlyInstanceOf(LinkedHashMap.class) + .containsOnly(entry(1, "value 1"), entry(2, "value 2")); + } + + @Test + void givenMap_whenUsingPutWithForLoop_thenInsertIntoLinkedHashMap() { + Map result = new LinkedHashMap<>(); + + for (Map.Entry entry : map.entrySet()) { + result.put(entry.getKey(), entry.getValue()); + } + + assertThat(result).isExactlyInstanceOf(LinkedHashMap.class) + .containsOnly(entry(1, "value 1"), entry(2, "value 2")); + } + + @Test + void givenMap_whenUsingPutWithMapForEach_thenInsertIntoLinkedHashMap() { + Map result = new LinkedHashMap<>(); + + map.forEach((k, v) -> result.put(k, v)); + + assertThat(result).isExactlyInstanceOf(LinkedHashMap.class) + .containsOnly(entry(1, "value 1"), entry(2, "value 2")); + } + + @Test + void givenMap_whenUsingPutWithSetForEach_thenInsertIntoLinkedHashMap() { + Map result = new LinkedHashMap<>(); + + map.entrySet() + .forEach(entry -> result.put(entry.getKey(), entry.getValue())); + + assertThat(result).isExactlyInstanceOf(LinkedHashMap.class) + .containsOnly(entry(1, "value 1"), entry(2, "value 2")); + } + + @Test + void givenMap_whenUsingPutWithStreamForEach_thenInsertIntoLinkedHashMapp() { + Map result = new LinkedHashMap<>(); + + map.entrySet() + .stream() + .forEach(entry -> result.put(entry.getKey(), entry.getValue())); + + assertThat(result).isExactlyInstanceOf(LinkedHashMap.class) + .containsOnly(entry(1, "value 1"), entry(2, "value 2")); + } + + @BeforeEach + void init() { + map = Map.of(1, "value 1", 2, "value 2"); + } +} \ No newline at end of file