Update StreamToMapAndMultiMapUnitTest.java (#15146)

This commit is contained in:
Mo Helmy 2023-11-06 19:34:00 +02:00 committed by GitHub
parent d847f0d335
commit 946f849767
1 changed files with 10 additions and 9 deletions

View File

@ -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<String> stringStream = Stream.of("one", "two", "three", "two");
LinkedHashMultimap<Object, Object> multimap = LinkedHashMultimap.create();
ListMultimap<String, String> 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<Object, Object> expectedMultimap = LinkedHashMultimap.create();
ListMultimap<String, String> 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);
}
}
}