[JAVA-29409] (#15483)

This commit is contained in:
panos-kakos 2023-12-26 06:47:23 +02:00 committed by GitHub
parent b83af82bce
commit 21347e6cf2
5 changed files with 111 additions and 2 deletions

View File

@ -11,4 +11,4 @@ This module contains articles about the Stream API in Java.
- [Should We Close a Java Stream?](https://www.baeldung.com/java-stream-close)
- [Returning Stream vs. Collection](https://www.baeldung.com/java-return-stream-collection)
- [Convert a Java Enumeration Into a Stream](https://www.baeldung.com/java-enumeration-to-stream)
- More articles: [[<-- prev>]](/../core-java-streams-2)
- More articles: [[<-- prev>]](/../core-java-streams-2) [[next -->]](/../core-java-streams-4)

View File

@ -10,3 +10,4 @@
- [Understanding the Difference Between Stream.of() and IntStream.range()](https://www.baeldung.com/java-stream-of-and-intstream-range)
- [Check if Object Is an Array in Java](https://www.baeldung.com/java-check-if-object-is-an-array)
- [Mapping an Array of Integers to Strings Using Java Streams](https://www.baeldung.com/java-stream-integer-array-to-strings)
- More articles: [[<-- prev>]](/../core-java-streams-3) [[next -->]](/../core-java-streams-5)

View File

@ -6,7 +6,7 @@
- [Partition a Stream in Java](https://www.baeldung.com/java-partition-stream)
- [Taking Every N-th Element from Finite and Infinite Streams in Java](https://www.baeldung.com/java-nth-element-finite-infinite-streams)
- [Modifying Objects Within Stream While Iterating](https://www.baeldung.com/java-stream-modify-objects-during-iteration)
- [Convert a Stream into a Map or Multimap in Java](https://www.baeldung.com/java-convert-stream-map-multimap)
- [How to Avoid NoSuchElementException in Stream API](https://www.baeldung.com/java-streams-api-avoid-nosuchelementexception)
- [Get Index of First Element Matching Boolean Using Java Streams](https://www.baeldung.com/java-streams-find-first-match-index)
- [Handling NullPointerException in findFirst() When the First Element Is Null](https://www.baeldung.com/java-handle-nullpointerexception-findfirst-first-null)
- More articles: [[<-- prev>]](/../core-java-streams-4)

View File

@ -1,2 +1,3 @@
## Relevant Articles:
- [Handle Duplicate Keys When Producing Map Using Java Stream](https://www.baeldung.com/java-duplicate-keys-when-producing-map-using-stream)
- [Convert a Stream into a Map or Multimap in Java](https://www.baeldung.com/java-convert-stream-map-multimap)

View File

@ -0,0 +1,107 @@
package com.baeldung.streams.streamtomapandmultimap;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
public class StreamToMapAndMultiMapUnitTest {
@Test
public void givenStringStream_whenConvertingToMapWithMerge_thenExpectedMapIsGenerated() {
Stream<String> stringStream = Stream.of("one", "two", "three", "two");
Map<String, String> mergedMap = stringStream.collect(
Collectors.toMap(s -> s, s -> s, (s1, s2) -> s1 + ", " + s2)
);
// Define the expected map
Map<String, String> expectedMap = Map.of(
"one", "one",
"two", "two, two",
"three", "three"
);
assertEquals(expectedMap, mergedMap);
}
@Test
public void givenStringStream_whenConvertingToMultimap_thenExpectedMultimapIsGenerated() {
Stream<String> stringStream = Stream.of("one", "two", "three", "two");
ListMultimap<String, String> multimap = stringStream.collect(
ArrayListMultimap::create,
(map, element) -> map.put(element, element),
ArrayListMultimap::putAll
);
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);
}
@Test
public void givenStringStream_whenConvertingToMultimapWithStreamReduce_thenExpectedMultimapIsGenerated() {
Stream<String> stringStream = Stream.of("one", "two", "three", "two");
Map<String, List<String>> multimap = stringStream.reduce(
new HashMap<>(),
(map, element) -> {
map.computeIfAbsent(element, k -> new ArrayList<>()).add(element);
return map;
},
(map1, map2) -> {
map2.forEach((key, value) -> map1.merge(key, value, (list1, list2) -> {
list1.addAll(list2);
return list1;
}));
return map1;
}
);
Map<String, List<String>> expectedMultimap = new HashMap<>();
expectedMultimap.put("one", Collections.singletonList("one"));
expectedMultimap.put("two", Arrays.asList("two", "two"));
expectedMultimap.put("three", Collections.singletonList("three"));
assertEquals(expectedMultimap, multimap);
}
@Test
public void givenStringStream_whenConvertingToMapWithStreamReduce_thenExpectedMapIsGenerated() {
Stream<String> stringStream = Stream.of("one", "two", "three", "two");
Map<String, String> resultMap = stringStream.reduce(
new HashMap<>(),
(map, element) -> {
map.put(element, element);
return map;
},
(map1, map2) -> {
map1.putAll(map2);
return map1;
}
);
Map<String, String> expectedMap = new HashMap<>();
expectedMap.put("one", "one");
expectedMap.put("two", "two");
expectedMap.put("three", "three");
assertEquals(expectedMap, resultMap);
}
}