diff --git a/core-java-modules/core-java-collections-set-2/pom.xml b/core-java-modules/core-java-collections-set-2/pom.xml new file mode 100644 index 0000000000..5f4ab83eae --- /dev/null +++ b/core-java-modules/core-java-collections-set-2/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + core-java-collections-set-2 + 0.0.1-SNAPSHOT + core-java-collections-set-2 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + 8 + 8 + + + diff --git a/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/treeset/collectintotreeset/Player.java b/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/treeset/collectintotreeset/Player.java new file mode 100644 index 0000000000..f46ab2e7e3 --- /dev/null +++ b/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/treeset/collectintotreeset/Player.java @@ -0,0 +1,36 @@ +package com.baeldung.treeset.collectintotreeset; + +public class Player implements Comparable { + 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); + } +} diff --git a/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/treeset/collectintotreeset/CollectIntoTreeSetUnitTest.java b/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/treeset/collectintotreeset/CollectIntoTreeSetUnitTest.java new file mode 100644 index 0000000000..403f39340c --- /dev/null +++ b/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/treeset/collectintotreeset/CollectIntoTreeSetUnitTest.java @@ -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 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 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 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 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); + } +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 9c562e8b28..a6ad43676d 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -34,6 +34,7 @@ core-java-collections-array-list core-java-collections-conversions core-java-collections-conversions-2 + core-java-collections-set-2 core-java-collections-list core-java-collections-list-2 core-java-collections-list-3 @@ -147,4 +148,4 @@ - \ No newline at end of file +