From 946f84976785a7942782d14fed6121ba48ec86ed Mon Sep 17 00:00:00 2001 From: Mo Helmy <135069400+BenHelmyBen@users.noreply.github.com> Date: Mon, 6 Nov 2023 19:34:00 +0200 Subject: [PATCH] Update StreamToMapAndMultiMapUnitTest.java (#15146) --- .../StreamToMapAndMultiMapUnitTest.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streamtomapandmultimap/StreamToMapAndMultiMapUnitTest.java b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streamtomapandmultimap/StreamToMapAndMultiMapUnitTest.java index 4d957fd704..f5da353551 100644 --- a/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streamtomapandmultimap/StreamToMapAndMultiMapUnitTest.java +++ b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streamtomapandmultimap/StreamToMapAndMultiMapUnitTest.java @@ -1,6 +1,7 @@ package com.baeldung.streamtomapandmultimap; -import com.google.common.collect.LinkedHashMultimap; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ListMultimap; import org.junit.Test; import java.util.*; @@ -32,16 +33,16 @@ public class StreamToMapAndMultiMapUnitTest { public void givenStringStream_whenConvertingToMultimap_thenExpectedMultimapIsGenerated() { Stream stringStream = Stream.of("one", "two", "three", "two"); - LinkedHashMultimap multimap = LinkedHashMultimap.create(); + ListMultimap multimap = stringStream.collect( + ArrayListMultimap::create, + (map, element) -> map.put(element, element), + ArrayListMultimap::putAll + ); - stringStream.collect(Collectors.groupingBy( - s -> s, - Collectors.mapping(s -> s, Collectors.toList()) - )).forEach((key, value) -> multimap.putAll(key, value)); - - LinkedHashMultimap expectedMultimap = LinkedHashMultimap.create(); + ListMultimap expectedMultimap = ArrayListMultimap.create(); expectedMultimap.put("one", "one"); expectedMultimap.put("two", "two"); + expectedMultimap.put("two", "two"); expectedMultimap.put("three", "three"); assertEquals(expectedMultimap, multimap); @@ -97,4 +98,4 @@ public class StreamToMapAndMultiMapUnitTest { assertEquals(expectedMap, resultMap); } -} \ No newline at end of file +}