Refactor Java8 examples

This commit is contained in:
Grzegorz Piwowarek 2016-07-14 10:00:07 +03:00
parent 2d0f3b2c7d
commit 77d37f2a88

View File

@ -25,7 +25,7 @@ import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.counting; import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining; 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.partitioningBy;
import static java.util.stream.Collectors.summarizingDouble; import static java.util.stream.Collectors.summarizingDouble;
import static java.util.stream.Collectors.summingDouble; import static java.util.stream.Collectors.summingDouble;
@ -38,10 +38,10 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class Java8CollectorsTest { public class Java8CollectorsTest {
private final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dd");
@Test @Test
public void whenCollectingToList_shouldCollectToList() throws Exception { public void whenCollectingToList_shouldCollectToList() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
final List<String> result = givenList.stream() final List<String> result = givenList.stream()
.collect(toList()); .collect(toList());
@ -51,8 +51,6 @@ public class Java8CollectorsTest {
@Test @Test
public void whenCollectingToList_shouldCollectToSet() throws Exception { public void whenCollectingToList_shouldCollectToSet() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
final Set<String> result = givenList.stream() final Set<String> result = givenList.stream()
.collect(toSet()); .collect(toSet());
@ -62,8 +60,6 @@ public class Java8CollectorsTest {
@Test @Test
public void whenCollectingToCollection_shouldCollectToCollection() throws Exception { public void whenCollectingToCollection_shouldCollectToCollection() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
final List<String> result = givenList.stream() final List<String> result = givenList.stream()
.collect(toCollection(LinkedList::new)); .collect(toCollection(LinkedList::new));
@ -74,8 +70,6 @@ public class Java8CollectorsTest {
@Test @Test
public void whenCollectingToImmutableCollection_shouldThrowException() throws Exception { public void whenCollectingToImmutableCollection_shouldThrowException() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
assertThatThrownBy(() -> { assertThatThrownBy(() -> {
givenList.stream() givenList.stream()
.collect(toCollection(ImmutableList::of)); .collect(toCollection(ImmutableList::of));
@ -85,8 +79,6 @@ public class Java8CollectorsTest {
@Test @Test
public void whenCollectingToMap_shouldCollectToMap() throws Exception { public void whenCollectingToMap_shouldCollectToMap() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
final Map<String, Integer> result = givenList.stream() final Map<String, Integer> result = givenList.stream()
.collect(toMap(Function.identity(), String::length)); .collect(toMap(Function.identity(), String::length));
@ -94,13 +86,11 @@ public class Java8CollectorsTest {
.containsEntry("a", 1) .containsEntry("a", 1)
.containsEntry("bb", 2) .containsEntry("bb", 2)
.containsEntry("ccc", 3) .containsEntry("ccc", 3)
.containsEntry("dddd", 4); .containsEntry("dd", 2);
} }
@Test @Test
public void whenCollectingToMap_shouldCollectToMapMerging() throws Exception { public void whenCollectingToMap_shouldCollectToMapMerging() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
final Map<String, Integer> result = givenList.stream() final Map<String, Integer> result = givenList.stream()
.collect(toMap(Function.identity(), String::length, (i1, i2) -> i1)); .collect(toMap(Function.identity(), String::length, (i1, i2) -> i1));
@ -108,13 +98,11 @@ public class Java8CollectorsTest {
.containsEntry("a", 1) .containsEntry("a", 1)
.containsEntry("bb", 2) .containsEntry("bb", 2)
.containsEntry("ccc", 3) .containsEntry("ccc", 3)
.containsEntry("dddd", 4); .containsEntry("dd", 2);
} }
@Test @Test
public void whenCollectingAndThen_shouldCollect() throws Exception { public void whenCollectingAndThen_shouldCollect() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
final List<String> result = givenList.stream() final List<String> result = givenList.stream()
.collect(collectingAndThen(toList(), ImmutableList::copyOf)); .collect(collectingAndThen(toList(), ImmutableList::copyOf));
@ -125,41 +113,33 @@ public class Java8CollectorsTest {
@Test @Test
public void whenJoining_shouldJoin() throws Exception { public void whenJoining_shouldJoin() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
final String result = givenList.stream() final String result = givenList.stream()
.collect(joining()); .collect(joining());
assertThat(result) assertThat(result)
.isEqualTo("abbcccdddd"); .isEqualTo("abbcccdd");
} }
@Test @Test
public void whenJoiningWithSeparator_shouldJoinWithSeparator() throws Exception { public void whenJoiningWithSeparator_shouldJoinWithSeparator() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
final String result = givenList.stream() final String result = givenList.stream()
.collect(joining(" ")); .collect(joining(" "));
assertThat(result) assertThat(result)
.isEqualTo("a bb ccc dddd"); .isEqualTo("a bb ccc dd");
} }
@Test @Test
public void whenJoiningWithSeparatorAndPrefixAndPostfix_shouldJoinWithSeparatorPrePost() throws Exception { public void whenJoiningWithSeparatorAndPrefixAndPostfix_shouldJoinWithSeparatorPrePost() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
final String result = givenList.stream() final String result = givenList.stream()
.collect(joining(" ", "PRE-", "-POST")); .collect(joining(" ", "PRE-", "-POST"));
assertThat(result) assertThat(result)
.isEqualTo("PRE-a bb ccc dddd-POST"); .isEqualTo("PRE-a bb ccc dd-POST");
} }
@Test @Test
public void whenPartitioningBy_shouldPartition() throws Exception { public void whenPartitioningBy_shouldPartition() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
final Map<Boolean, List<String>> result = givenList.stream() final Map<Boolean, List<String>> result = givenList.stream()
.collect(partitioningBy(s -> s.length() > 2)); .collect(partitioningBy(s -> s.length() > 2));
@ -167,17 +147,15 @@ public class Java8CollectorsTest {
.containsKeys(true, false) .containsKeys(true, false)
.satisfies(booleanListMap -> { .satisfies(booleanListMap -> {
assertThat(booleanListMap.get(true)) assertThat(booleanListMap.get(true))
.contains("ccc", "dddd"); .contains("ccc");
assertThat(booleanListMap.get(false)) assertThat(booleanListMap.get(false))
.contains("a", "bb"); .contains("a", "bb", "dd");
}); });
} }
@Test @Test
public void whenCounting_shouldCount() throws Exception { public void whenCounting_shouldCount() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
final Long result = givenList.stream() final Long result = givenList.stream()
.collect(counting()); .collect(counting());
@ -187,10 +165,8 @@ public class Java8CollectorsTest {
@Test @Test
public void whenSummarizing_shouldSummarize() throws Exception { public void whenSummarizing_shouldSummarize() throws Exception {
final List<Double> givenList = Arrays.asList(1d, 2d, 3d, 2d);
final DoubleSummaryStatistics result = givenList.stream() final DoubleSummaryStatistics result = givenList.stream()
.collect(summarizingDouble(d -> d)); .collect(summarizingDouble(String::length));
assertThat(result.getAverage()) assertThat(result.getAverage())
.isEqualTo(2); .isEqualTo(2);
@ -206,10 +182,8 @@ public class Java8CollectorsTest {
@Test @Test
public void whenAveraging_shouldAverage() throws Exception { public void whenAveraging_shouldAverage() throws Exception {
final List<Double> givenList = Arrays.asList(1d, 2d, 3d, 2d);
final Double result = givenList.stream() final Double result = givenList.stream()
.collect(averagingDouble(d -> d)); .collect(averagingDouble(String::length));
assertThat(result) assertThat(result)
.isEqualTo(2); .isEqualTo(2);
@ -217,10 +191,8 @@ public class Java8CollectorsTest {
@Test @Test
public void whenSumming_shouldSum() throws Exception { public void whenSumming_shouldSum() throws Exception {
final List<Double> givenList = Arrays.asList(1d, 2d, 3d, 2d);
final Double result = givenList.stream() final Double result = givenList.stream()
.collect(summingDouble(d -> d)); .collect(summingDouble(String::length));
assertThat(result) assertThat(result)
.isEqualTo(8); .isEqualTo(8);
@ -228,41 +200,34 @@ public class Java8CollectorsTest {
@Test @Test
public void whenMaxingBy_shouldMaxBy() throws Exception { public void whenMaxingBy_shouldMaxBy() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
final Optional<String> result = givenList.stream() final Optional<String> result = givenList.stream()
.collect(maxBy(Comparator.naturalOrder())); .collect(maxBy(Comparator.naturalOrder()));
assertThat(result) assertThat(result)
.isPresent() .isPresent()
.hasValue("dddd"); .hasValue("dd");
} }
@Test @Test
public void whenGroupingBy_shouldGroupBy() throws Exception { public void whenGroupingBy_shouldGroupBy() throws Exception {
final List<String> givenList = Arrays.asList("a", "b", "bb", "ccc", "dddd");
final Map<Integer, Set<String>> result = givenList.stream() final Map<Integer, Set<String>> result = givenList.stream()
.collect(groupingBy(String::length, toSet())); .collect(groupingBy(String::length, toSet()));
assertThat(result) assertThat(result)
.containsEntry(1, newHashSet("a", "b")) .containsEntry(1, newHashSet("a"))
.containsEntry(2, newHashSet("bb")) .containsEntry(2, newHashSet("bb", "dd"))
.containsEntry(3, newHashSet("ccc")) .containsEntry(3, newHashSet("ccc"));
.containsEntry(4, newHashSet("dddd"));
} }
@Test @Test
public void whenCreatingCustomCollector_shouldCollect() throws Exception { public void whenCreatingCustomCollector_shouldCollect() throws Exception {
final List<String> givenList = Arrays.asList("a", "bb", "ccc", "dddd");
final ImmutableSet<String> result = givenList.stream() final ImmutableSet<String> result = givenList.stream()
.collect(toImmutableSet()); .collect(toImmutableSet());
assertThat(result) assertThat(result)
.isInstanceOf(ImmutableSet.class) .isInstanceOf(ImmutableSet.class)
.contains("a", "bb", "ccc", "dddd"); .contains("a", "bb", "ccc", "dd");
} }