[collect-into-treeSet] Using Streams to Collect into a TreeSet (#12834)

* [collect-into-treeSet] Using Streams to Collect into a TreeSet

* [collect-into-treeSet] move to core-java-collections-set-2
This commit is contained in:
Kai Yuan 2022-10-11 02:58:20 +02:00 committed by GitHub
parent 789539408d
commit 6381f2b8ba
4 changed files with 129 additions and 1 deletions

View File

@ -0,0 +1,39 @@
<?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-collections-set-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>core-java-collections-set-2</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>
</dependencies>
<build>
<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>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,36 @@
package com.baeldung.treeset.collectintotreeset;
public class Player implements Comparable<Player> {
private String name;
private int age;
private int numberOfPlayed;
private int numberOfWins;
public Player(String name, int age, int numberOfPlayed, int numberOfWins) {
this.name = name;
this.age = age;
this.numberOfPlayed = numberOfPlayed;
this.numberOfWins = numberOfWins;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getNumberOfPlayed() {
return numberOfPlayed;
}
public int getNumberOfWins() {
return numberOfWins;
}
@Override
public int compareTo(Player o) {
return Integer.compare(age, o.age);
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.treeset.collectintotreeset;
import static org.assertj.core.api.Assertions.assertThat;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Comparator;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
public class CollectIntoTreeSetUnitTest {
Player kai = new Player("Kai", 26, 28, 7);
Player eric = new Player("Eric", 28, 30, 11);
Player saajan = new Player("Saajan", 30, 100, 66);
Player kevin = new Player("Kevin", 24, 50, 49);
@Test
void givenAStream_whenCollectIntoTreeSetWithNaturalOrder_thenGetExpectedResult() {
String kotlin = "Kotlin";
String java = "Java";
String python = "Python";
String ruby = "Ruby";
TreeSet<String> myTreeSet = Stream.of(ruby, java, kotlin, python)
.collect(Collectors.toCollection(TreeSet::new));
assertThat(myTreeSet).containsExactly(java, kotlin, python, ruby);
}
@Test
void givenAPlayerStream_whenCollectIntoTreeSet_thenGetExpectedResult() {
TreeSet<Player> myTreeSet = Stream.of(saajan, eric, kai, kevin)
.collect(Collectors.toCollection(TreeSet::new));
assertThat(myTreeSet).containsExactly(kevin, kai, eric, saajan);
}
@Test
void givenAPlayerStream_whenCollectIntoTreeSetWithNoOfWinsComparator_thenGetExpectedResult() {
TreeSet<Player> myTreeSet = Stream.of(saajan, eric, kai, kevin)
.collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparingInt(Player::getNumberOfWins))));
assertThat(myTreeSet).containsExactly(kai, eric, kevin, saajan);
}
@Test
void givenAPlayerStream_whenCollectIntoTreeSetWithWinRateComparator_thenGetExpectedResult() {
TreeSet<Player> myTreeSet = Stream.of(saajan, eric, kai, kevin)
.collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(player -> BigDecimal.valueOf(player.getNumberOfWins())
.divide(BigDecimal.valueOf(player.getNumberOfPlayed()), 2, RoundingMode.HALF_UP)))));
assertThat(myTreeSet).containsExactly(kai, eric, saajan, kevin);
}
}

View File

@ -34,6 +34,7 @@
<module>core-java-collections-array-list</module> <module>core-java-collections-array-list</module>
<module>core-java-collections-conversions</module> <module>core-java-collections-conversions</module>
<module>core-java-collections-conversions-2</module> <module>core-java-collections-conversions-2</module>
<module>core-java-collections-set-2</module>
<module>core-java-collections-list</module> <module>core-java-collections-list</module>
<module>core-java-collections-list-2</module> <module>core-java-collections-list-2</module>
<module>core-java-collections-list-3</module> <module>core-java-collections-list-3</module>
@ -147,4 +148,4 @@
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
</project> </project>