[stream-2maap-dup-keys] Handle Duplicated Keys When Producing Map Usi… (#13546)

* [stream-2maap-dup-keys] Handle Duplicated Keys When Producing Map Using Java Streamt

* [stream-2maap-dup-keys] create the *-streams-maps module

* [stream-2maap-dup-keys] add the new moudle to the parent pom
This commit is contained in:
Kai Yuan 2023-03-09 02:37:48 +01:00 committed by GitHub
parent 7be831c312
commit 0f8f3e0444
4 changed files with 166 additions and 0 deletions

View File

@ -0,0 +1 @@
## Relevant Articles:

View File

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-streams-maps</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-streams-maps</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit-jupiter.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.23.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-streams-maps</finalName>
<resources>
<resource>
<directory>src/main</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<!-- testing -->
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,97 @@
package com.baeldung.streams.tomap;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
class City {
private String name;
private String locatedIn;
public City(String name, String locatedIn) {
this.name = name;
this.locatedIn = locatedIn;
}
public String getName() {
return name;
}
public String getLocatedIn() {
return locatedIn;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof City))
return false;
City city = (City) o;
if (!name.equals(city.name))
return false;
return locatedIn.equals(city.locatedIn);
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + locatedIn.hashCode();
return result;
}
}
public class StreamToMapDuplicatedKeysHandlingUnitTest {
// @formatter:off
private final List<City> CITY_INPUT = Arrays.asList(
new City("New York City", "USA"),
new City("Shanghai", "China"),
new City("Hamburg", "Germany"),
new City("Paris", "France"),
new City("Paris", "Texas, USA"));
// @formatter:on
@Test
void givenCityList_whenUsingGroupingBy_shouldContainExpectedCity() {
Map<String, List<City>> resultMap = CITY_INPUT.stream()
.collect(groupingBy(City::getName));
assertEquals(4, resultMap.size());
// @formatter:off
assertEquals(Arrays.asList(
new City("Paris", "France"),
new City("Paris", "Texas, USA")), resultMap.get("Paris"));
// @formatter:on
}
@Test
void givenCityList_whenContainingDuplicatedNamesUsingToMap_shouldContainExpectedCity() {
Map<String, City> resultMap1 = CITY_INPUT.stream()
.collect(toMap(City::getName, Function.identity(), (first, second) -> first));
assertEquals(4, resultMap1.size());
assertEquals(new City("Paris", "France"), resultMap1.get("Paris"));
Map<String, City> resultMap2 = CITY_INPUT.stream()
.collect(toMap(City::getName, Function.identity(), (first, second) -> second));
assertEquals(4, resultMap2.size());
assertEquals(new City("Paris", "Texas, USA"), resultMap2.get("Paris"));
Map<String, City> resultMap3 = CITY_INPUT.stream()
.collect(toMap(City::getName, Function.identity(), (first, second) -> {
String locations = first.getLocatedIn() + " and " + second.getLocatedIn();
return new City(first.getName(), locations);
}));
assertEquals(4, resultMap2.size());
assertEquals(new City("Paris", "France and Texas, USA"), resultMap3.get("Paris"));
}
}

View File

@ -120,6 +120,7 @@
<module>core-java-streams</module>
<module>core-java-streams-2</module>
<module>core-java-streams-3</module>
<module>core-java-streams-maps</module>
<module>core-java-string-algorithms</module>
<module>core-java-string-algorithms-2</module>
<module>core-java-string-apis</module>