From 2d0f3b2c7d491117d878c701519f48665dd4be97 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Wed, 13 Jul 2016 09:28:36 +0300 Subject: [PATCH] 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); + } + } }