From 92d4f6cbb625f44229b1b0019a3a9f79a6cbb103 Mon Sep 17 00:00:00 2001 From: DOHA Date: Thu, 14 Jan 2016 12:18:51 +0200 Subject: [PATCH] add partition list examples --- .../java/CollectionJavaPartitionUnitTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 guava/src/test/java/org/baeldung/java/CollectionJavaPartitionUnitTest.java diff --git a/guava/src/test/java/org/baeldung/java/CollectionJavaPartitionUnitTest.java b/guava/src/test/java/org/baeldung/java/CollectionJavaPartitionUnitTest.java new file mode 100644 index 0000000000..639aea58f6 --- /dev/null +++ b/guava/src/test/java/org/baeldung/java/CollectionJavaPartitionUnitTest.java @@ -0,0 +1,74 @@ +package org.baeldung.java; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import org.junit.Test; + +import com.google.common.collect.Lists; + +public class CollectionJavaPartitionUnitTest { + + // java8 groupBy + @Test + public final void givenList_whenParitioningIntoNSublistsUsingGroupingBy_thenCorrect() { + final List intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); + + final Map> groups = intList.stream().collect(Collectors.groupingBy(s -> (s - 1) / 3)); + final List> subSets = new ArrayList>(groups.values()); + + // When + final List lastPartition = subSets.get(2); + final List expectedLastPartition = Lists. newArrayList(7, 8); + assertThat(subSets.size(), equalTo(3)); + assertThat(lastPartition, equalTo(expectedLastPartition)); + + // intList.add(9); + // System.out.println(groups.values()); + } + + // java8 partitionBy + @Test + public final void givenList_whenParitioningIntoSublistsUsingPartitionBy_thenCorrect() { + final List intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); + + final Map> groups = intList.stream().collect(Collectors.partitioningBy(s -> s > 6)); + final List> subSets = new ArrayList>(groups.values()); + + // When + final List lastPartition = subSets.get(1); + final List expectedLastPartition = Lists. newArrayList(7, 8); + assertThat(subSets.size(), equalTo(2)); + assertThat(lastPartition, equalTo(expectedLastPartition)); + + // intList.add(9); + // System.out.println(groups.values()); + } + + // java8 split by separator + @Test + public final void givenList_whenSplittingBySeparator_thenCorrect() { + final List intList = Lists.newArrayList(1, 2, 3, 0, 4, 5, 6, 0, 7, 8); + + final int[] indexes = Stream.of(IntStream.of(-1), IntStream.range(0, intList.size()).filter(i -> intList.get(i) == 0), IntStream.of(intList.size())).flatMapToInt(s -> s).toArray(); + final List> subSets = IntStream.range(0, indexes.length - 1).mapToObj(i -> intList.subList(indexes[i] + 1, indexes[i + 1])).collect(Collectors.toList()); + + // When + final List lastPartition = subSets.get(2); + final List expectedLastPartition = Lists. newArrayList(7, 8); + assertThat(subSets.size(), equalTo(3)); + assertThat(lastPartition, equalTo(expectedLastPartition)); + } + + + + + +}