From 5bfb12d09101951c668ba8cbc82d6eee10aa49c8 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Wed, 13 Jul 2016 02:02:56 +0300 Subject: [PATCH 1/6] Remove unnecessary file --- core-java-8/.project | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 core-java-8/.project diff --git a/core-java-8/.project b/core-java-8/.project deleted file mode 100644 index a23ff42ae0..0000000000 --- a/core-java-8/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - core-java8 - - - - - - org.eclipse.m2e.core.maven2Builder - - - - - - org.eclipse.m2e.core.maven2Nature - - From e93772ae605aabfeb95efc86ad7c1ff465e8f433 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Wed, 13 Jul 2016 02:13:26 +0300 Subject: [PATCH 2/6] TDD --- core-java-8/pom.xml | 3 +- .../collectors/Java8CollectorsTest.java | 76 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java diff --git a/core-java-8/pom.xml b/core-java-8/pom.xml index 8c9bb36f7d..63df0e1b95 100644 --- a/core-java-8/pom.xml +++ b/core-java-8/pom.xml @@ -66,7 +66,8 @@ org.assertj assertj-core - 3.4.1 + 3.5.1 + test diff --git a/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java b/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java new file mode 100644 index 0000000000..bfcdf27a9c --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java @@ -0,0 +1,76 @@ +package com.baeldung.collectors; + +import org.junit.Test; + +public class Java8CollectorsTest { + + @Test + public void whenCollectingToList_shouldCollectToList() throws Exception { + + } + + @Test + public void whenCollectingToList_shouldCollectToSet() throws Exception { + + } + + @Test + public void whenCollectingToCollection_shouldCollectToCollection() throws Exception { + + } + + @Test + public void whenCollectingToImmutableCollection_shouldThrowException() throws Exception { + + } + + @Test + public void whenCollectingToMap_shouldCollectToMap() throws Exception { + + } + + @Test + public void whenCollectingAndThen_shouldCollect() throws Exception { + + } + + @Test + public void whenJoining_shouldJoin() throws Exception { + + } + + @Test + public void whenPartitioningBy_shouldPartition() throws Exception { + + } + + @Test + public void whenCounting_shouldCount() throws Exception { + + } + + @Test + public void whenSummarizing_shouldSummarize() throws Exception { + + } + + @Test + public void whenAveraging_shouldAverage() throws Exception { + + } + + @Test + public void whenSumming_shouldSum() throws Exception { + + } + + @Test + public void whenMaxingBy_shouldMaxBy() throws Exception { + + } + + @Test + public void whenCreatingCustomCollector_shouldCollect() throws Exception { + + } +} From 2d0f3b2c7d491117d878c701519f48665dd4be97 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Wed, 13 Jul 2016 09:28:36 +0300 Subject: [PATCH 3/6] Add Java8 Collectors examples --- .../collectors/Java8CollectorsTest.java | 224 ++++++++++++++++++ 1 file changed, 224 insertions(+) diff --git a/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java b/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java index bfcdf27a9c..11699463d5 100644 --- a/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java +++ b/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java @@ -1,76 +1,300 @@ package com.baeldung.collectors; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; import org.junit.Test; +import java.util.Arrays; +import java.util.Comparator; +import java.util.DoubleSummaryStatistics; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Collector; + +import static com.google.common.collect.Sets.newHashSet; +import static java.util.stream.Collectors.averagingDouble; +import static java.util.stream.Collectors.collectingAndThen; +import static java.util.stream.Collectors.counting; +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.joining; +import static java.util.stream.Collectors.maxBy; +import static java.util.stream.Collectors.partitioningBy; +import static java.util.stream.Collectors.summarizingDouble; +import static java.util.stream.Collectors.summingDouble; +import static java.util.stream.Collectors.toCollection; +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toMap; +import static java.util.stream.Collectors.toSet; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + public class Java8CollectorsTest { @Test public void whenCollectingToList_shouldCollectToList() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + final List result = givenList.stream() + .collect(toList()); + + assertThat(result) + .containsAll(givenList); } @Test public void whenCollectingToList_shouldCollectToSet() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + final Set result = givenList.stream() + .collect(toSet()); + + assertThat(result) + .containsAll(givenList); } @Test public void whenCollectingToCollection_shouldCollectToCollection() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + final List result = givenList.stream() + .collect(toCollection(LinkedList::new)); + + assertThat(result) + .containsAll(givenList) + .isInstanceOf(LinkedList.class); } @Test public void whenCollectingToImmutableCollection_shouldThrowException() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + + assertThatThrownBy(() -> { + givenList.stream() + .collect(toCollection(ImmutableList::of)); + }).isInstanceOf(UnsupportedOperationException.class); } @Test public void whenCollectingToMap_shouldCollectToMap() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + final Map result = givenList.stream() + .collect(toMap(Function.identity(), String::length)); + + assertThat(result) + .containsEntry("a", 1) + .containsEntry("bb", 2) + .containsEntry("ccc", 3) + .containsEntry("dddd", 4); + } + + @Test + public void whenCollectingToMap_shouldCollectToMapMerging() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + + final Map result = givenList.stream() + .collect(toMap(Function.identity(), String::length, (i1, i2) -> i1)); + + assertThat(result) + .containsEntry("a", 1) + .containsEntry("bb", 2) + .containsEntry("ccc", 3) + .containsEntry("dddd", 4); } @Test public void whenCollectingAndThen_shouldCollect() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + final List result = givenList.stream() + .collect(collectingAndThen(toList(), ImmutableList::copyOf)); + + assertThat(result) + .containsAll(givenList) + .isInstanceOf(ImmutableList.class); } @Test public void whenJoining_shouldJoin() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + final String result = givenList.stream() + .collect(joining()); + + assertThat(result) + .isEqualTo("abbcccdddd"); + } + + @Test + public void whenJoiningWithSeparator_shouldJoinWithSeparator() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + + final String result = givenList.stream() + .collect(joining(" ")); + + assertThat(result) + .isEqualTo("a bb ccc dddd"); + } + + @Test + public void whenJoiningWithSeparatorAndPrefixAndPostfix_shouldJoinWithSeparatorPrePost() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + + final String result = givenList.stream() + .collect(joining(" ", "PRE-", "-POST")); + + assertThat(result) + .isEqualTo("PRE-a bb ccc dddd-POST"); } @Test public void whenPartitioningBy_shouldPartition() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + final Map> result = givenList.stream() + .collect(partitioningBy(s -> s.length() > 2)); + + assertThat(result) + .containsKeys(true, false) + .satisfies(booleanListMap -> { + assertThat(booleanListMap.get(true)) + .contains("ccc", "dddd"); + + assertThat(booleanListMap.get(false)) + .contains("a", "bb"); + }); } @Test public void whenCounting_shouldCount() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + final Long result = givenList.stream() + .collect(counting()); + + assertThat(result) + .isEqualTo(4); } @Test public void whenSummarizing_shouldSummarize() throws Exception { + final List givenList = Arrays.asList(1d, 2d, 3d, 2d); + final DoubleSummaryStatistics result = givenList.stream() + .collect(summarizingDouble(d -> d)); + + assertThat(result.getAverage()) + .isEqualTo(2); + assertThat(result.getCount()) + .isEqualTo(4); + assertThat(result.getMax()) + .isEqualTo(3); + assertThat(result.getMin()) + .isEqualTo(1); + assertThat(result.getSum()) + .isEqualTo(8); } @Test public void whenAveraging_shouldAverage() throws Exception { + final List givenList = Arrays.asList(1d, 2d, 3d, 2d); + final Double result = givenList.stream() + .collect(averagingDouble(d -> d)); + + assertThat(result) + .isEqualTo(2); } @Test public void whenSumming_shouldSum() throws Exception { + final List givenList = Arrays.asList(1d, 2d, 3d, 2d); + final Double result = givenList.stream() + .collect(summingDouble(d -> d)); + + assertThat(result) + .isEqualTo(8); } @Test public void whenMaxingBy_shouldMaxBy() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + final Optional result = givenList.stream() + .collect(maxBy(Comparator.naturalOrder())); + + assertThat(result) + .isPresent() + .hasValue("dddd"); } + @Test + public void whenGroupingBy_shouldGroupBy() throws Exception { + final List givenList = Arrays.asList("a", "b", "bb", "ccc", "dddd"); + + final Map> result = givenList.stream() + .collect(groupingBy(String::length, toSet())); + + assertThat(result) + .containsEntry(1, newHashSet("a", "b")) + .containsEntry(2, newHashSet("bb")) + .containsEntry(3, newHashSet("ccc")) + .containsEntry(4, newHashSet("dddd")); + } + + @Test public void whenCreatingCustomCollector_shouldCollect() throws Exception { + final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); + + final ImmutableSet result = givenList.stream() + .collect(toImmutableSet()); + + assertThat(result) + .isInstanceOf(ImmutableSet.class) + .contains("a", "bb", "ccc", "dddd"); } + + private static ImmutableSetCollector toImmutableSet() { + return new ImmutableSetCollector<>(); + } + + private static class ImmutableSetCollector implements Collector, ImmutableSet> { + + @Override + public Supplier> supplier() { + return ImmutableSet::builder; + } + + @Override + public BiConsumer, T> accumulator() { + return ImmutableSet.Builder::add; + } + + @Override + public BinaryOperator> combiner() { + return (left, right) -> left.addAll(right.build()); + } + + @Override + public Function, ImmutableSet> finisher() { + return ImmutableSet.Builder::build; + } + + @Override + public Set characteristics() { + return Sets.immutableEnumSet(Characteristics.UNORDERED); + } + } } From 77d37f2a88e529ad4ac9ccf003ade6436973a0b4 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Thu, 14 Jul 2016 10:00:07 +0300 Subject: [PATCH 4/6] Refactor Java8 examples --- .../collectors/Java8CollectorsTest.java | 71 +++++-------------- 1 file changed, 18 insertions(+), 53 deletions(-) diff --git a/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java b/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java index 11699463d5..b0609c9889 100644 --- a/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java +++ b/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java @@ -25,7 +25,7 @@ import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.counting; import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.joining; -import static java.util.stream.Collectors.maxBy; +import static java.util.stream.Collectors.*; import static java.util.stream.Collectors.partitioningBy; import static java.util.stream.Collectors.summarizingDouble; import static java.util.stream.Collectors.summingDouble; @@ -38,10 +38,10 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; public class Java8CollectorsTest { + private final List givenList = Arrays.asList("a", "bb", "ccc", "dd"); + @Test public void whenCollectingToList_shouldCollectToList() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - final List result = givenList.stream() .collect(toList()); @@ -51,8 +51,6 @@ public class Java8CollectorsTest { @Test public void whenCollectingToList_shouldCollectToSet() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - final Set result = givenList.stream() .collect(toSet()); @@ -62,8 +60,6 @@ public class Java8CollectorsTest { @Test public void whenCollectingToCollection_shouldCollectToCollection() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - final List result = givenList.stream() .collect(toCollection(LinkedList::new)); @@ -74,8 +70,6 @@ public class Java8CollectorsTest { @Test public void whenCollectingToImmutableCollection_shouldThrowException() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - assertThatThrownBy(() -> { givenList.stream() .collect(toCollection(ImmutableList::of)); @@ -85,8 +79,6 @@ public class Java8CollectorsTest { @Test public void whenCollectingToMap_shouldCollectToMap() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - final Map result = givenList.stream() .collect(toMap(Function.identity(), String::length)); @@ -94,13 +86,11 @@ public class Java8CollectorsTest { .containsEntry("a", 1) .containsEntry("bb", 2) .containsEntry("ccc", 3) - .containsEntry("dddd", 4); + .containsEntry("dd", 2); } @Test public void whenCollectingToMap_shouldCollectToMapMerging() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - final Map result = givenList.stream() .collect(toMap(Function.identity(), String::length, (i1, i2) -> i1)); @@ -108,13 +98,11 @@ public class Java8CollectorsTest { .containsEntry("a", 1) .containsEntry("bb", 2) .containsEntry("ccc", 3) - .containsEntry("dddd", 4); + .containsEntry("dd", 2); } @Test public void whenCollectingAndThen_shouldCollect() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - final List result = givenList.stream() .collect(collectingAndThen(toList(), ImmutableList::copyOf)); @@ -125,41 +113,33 @@ public class Java8CollectorsTest { @Test public void whenJoining_shouldJoin() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - final String result = givenList.stream() .collect(joining()); assertThat(result) - .isEqualTo("abbcccdddd"); + .isEqualTo("abbcccdd"); } @Test public void whenJoiningWithSeparator_shouldJoinWithSeparator() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - final String result = givenList.stream() .collect(joining(" ")); assertThat(result) - .isEqualTo("a bb ccc dddd"); + .isEqualTo("a bb ccc dd"); } @Test public void whenJoiningWithSeparatorAndPrefixAndPostfix_shouldJoinWithSeparatorPrePost() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - final String result = givenList.stream() .collect(joining(" ", "PRE-", "-POST")); assertThat(result) - .isEqualTo("PRE-a bb ccc dddd-POST"); + .isEqualTo("PRE-a bb ccc dd-POST"); } @Test public void whenPartitioningBy_shouldPartition() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - final Map> result = givenList.stream() .collect(partitioningBy(s -> s.length() > 2)); @@ -167,17 +147,15 @@ public class Java8CollectorsTest { .containsKeys(true, false) .satisfies(booleanListMap -> { assertThat(booleanListMap.get(true)) - .contains("ccc", "dddd"); + .contains("ccc"); assertThat(booleanListMap.get(false)) - .contains("a", "bb"); + .contains("a", "bb", "dd"); }); } @Test public void whenCounting_shouldCount() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - final Long result = givenList.stream() .collect(counting()); @@ -187,10 +165,8 @@ public class Java8CollectorsTest { @Test public void whenSummarizing_shouldSummarize() throws Exception { - final List givenList = Arrays.asList(1d, 2d, 3d, 2d); - final DoubleSummaryStatistics result = givenList.stream() - .collect(summarizingDouble(d -> d)); + .collect(summarizingDouble(String::length)); assertThat(result.getAverage()) .isEqualTo(2); @@ -206,10 +182,8 @@ public class Java8CollectorsTest { @Test public void whenAveraging_shouldAverage() throws Exception { - final List givenList = Arrays.asList(1d, 2d, 3d, 2d); - final Double result = givenList.stream() - .collect(averagingDouble(d -> d)); + .collect(averagingDouble(String::length)); assertThat(result) .isEqualTo(2); @@ -217,10 +191,8 @@ public class Java8CollectorsTest { @Test public void whenSumming_shouldSum() throws Exception { - final List givenList = Arrays.asList(1d, 2d, 3d, 2d); - final Double result = givenList.stream() - .collect(summingDouble(d -> d)); + .collect(summingDouble(String::length)); assertThat(result) .isEqualTo(8); @@ -228,41 +200,34 @@ public class Java8CollectorsTest { @Test public void whenMaxingBy_shouldMaxBy() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - final Optional result = givenList.stream() .collect(maxBy(Comparator.naturalOrder())); assertThat(result) .isPresent() - .hasValue("dddd"); + .hasValue("dd"); } @Test public void whenGroupingBy_shouldGroupBy() throws Exception { - final List givenList = Arrays.asList("a", "b", "bb", "ccc", "dddd"); - final Map> result = givenList.stream() .collect(groupingBy(String::length, toSet())); assertThat(result) - .containsEntry(1, newHashSet("a", "b")) - .containsEntry(2, newHashSet("bb")) - .containsEntry(3, newHashSet("ccc")) - .containsEntry(4, newHashSet("dddd")); + .containsEntry(1, newHashSet("a")) + .containsEntry(2, newHashSet("bb", "dd")) + .containsEntry(3, newHashSet("ccc")); } @Test public void whenCreatingCustomCollector_shouldCollect() throws Exception { - final List givenList = Arrays.asList("a", "bb", "ccc", "dddd"); - final ImmutableSet result = givenList.stream() .collect(toImmutableSet()); assertThat(result) .isInstanceOf(ImmutableSet.class) - .contains("a", "bb", "ccc", "dddd"); + .contains("a", "bb", "ccc", "dd"); } From af9296951559dfdc51d826cc0e026a23cb005b89 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Thu, 14 Jul 2016 10:20:47 +0300 Subject: [PATCH 5/6] Fix import --- .../test/java/com/baeldung/collectors/Java8CollectorsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java b/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java index b0609c9889..d94f72b685 100644 --- a/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java +++ b/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsTest.java @@ -25,7 +25,7 @@ import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.counting; import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.joining; -import static java.util.stream.Collectors.*; +import static java.util.stream.Collectors.maxBy; import static java.util.stream.Collectors.partitioningBy; import static java.util.stream.Collectors.summarizingDouble; import static java.util.stream.Collectors.summingDouble; From ea9cb32d6cc4de7d4b516120d066428fa79268c7 Mon Sep 17 00:00:00 2001 From: Slavisa Baeldung Date: Thu, 14 Jul 2016 15:03:51 +0200 Subject: [PATCH 6/6] BAEL-28 - Updating article URLs --- core-java-8/README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core-java-8/README.md b/core-java-8/README.md index b03d24c34e..e6bac2a4c9 100644 --- a/core-java-8/README.md +++ b/core-java-8/README.md @@ -3,9 +3,12 @@ ## Core Java 8 Cookbooks and Examples ### Relevant Articles: -// - [Java 8 – Powerful Comparison with Lambdas](http://www.baeldung.com/java-8-sort-lambda) +- [Java 8 – Powerful Comparison with Lambdas](http://www.baeldung.com/java-8-sort-lambda) - [Java – Directory Size](http://www.baeldung.com/java-folder-size) - [Java – Try with Resources](http://www.baeldung.com/java-try-with-resources) +- [A Guide to the Java ExecutorService](http://www.baeldung.com/java-executor-service-tutorial) +- [Java 8 New Features](http://www.baeldung.com/java-8-new-features) - [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips) - [The Double Colon Operator in Java 8](http://www.baeldung.com/java-8-double-colon-operator) -- [A Guide to the Java ExecutorService](http://www.baeldung.com/java-executor-service-tutorial) +- [Java 8 Streams Advanced](http://www.baeldung.com/java-8-streams) +- [Java 8 Collectors](http://www.baeldung.com/java-8-collectors) \ No newline at end of file