|
| 1 | +package org.baeldung.java; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.equalTo; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.stream.Collectors; |
| 10 | +import java.util.stream.IntStream; |
| 11 | +import java.util.stream.Stream; |
| 12 | + |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import com.google.common.collect.Lists; |
| 16 | + |
| 17 | +public class CollectionJavaPartitionUnitTest { |
| 18 | + |
| 19 | + // java8 groupBy |
| 20 | + @Test |
| 21 | + public final void givenList_whenParitioningIntoNSublistsUsingGroupingBy_thenCorrect() { |
| 22 | + final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); |
| 23 | + |
| 24 | + final Map<Integer, List<Integer>> groups = intList.stream().collect(Collectors.groupingBy(s -> (s - 1) / 3)); |
| 25 | + final List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values()); |
| 26 | + |
| 27 | + // When |
| 28 | + final List<Integer> lastPartition = subSets.get(2); |
| 29 | + final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8); |
| 30 | + assertThat(subSets.size(), equalTo(3)); |
| 31 | + assertThat(lastPartition, equalTo(expectedLastPartition)); |
| 32 | + |
| 33 | + // intList.add(9); |
| 34 | + // System.out.println(groups.values()); |
| 35 | + } |
| 36 | + |
| 37 | + // java8 partitionBy |
| 38 | + @Test |
| 39 | + public final void givenList_whenParitioningIntoSublistsUsingPartitionBy_thenCorrect() { |
| 40 | + final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); |
| 41 | + |
| 42 | + final Map<Boolean, List<Integer>> groups = intList.stream().collect(Collectors.partitioningBy(s -> s > 6)); |
| 43 | + final List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values()); |
| 44 | + |
| 45 | + // When |
| 46 | + final List<Integer> lastPartition = subSets.get(1); |
| 47 | + final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8); |
| 48 | + assertThat(subSets.size(), equalTo(2)); |
| 49 | + assertThat(lastPartition, equalTo(expectedLastPartition)); |
| 50 | + |
| 51 | + // intList.add(9); |
| 52 | + // System.out.println(groups.values()); |
| 53 | + } |
| 54 | + |
| 55 | + // java8 split by separator |
| 56 | + @Test |
| 57 | + public final void givenList_whenSplittingBySeparator_thenCorrect() { |
| 58 | + final List<Integer> intList = Lists.newArrayList(1, 2, 3, 0, 4, 5, 6, 0, 7, 8); |
| 59 | + |
| 60 | + 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(); |
| 61 | + final List<List<Integer>> subSets = IntStream.range(0, indexes.length - 1).mapToObj(i -> intList.subList(indexes[i] + 1, indexes[i + 1])).collect(Collectors.toList()); |
| 62 | + |
| 63 | + // When |
| 64 | + final List<Integer> lastPartition = subSets.get(2); |
| 65 | + final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8); |
| 66 | + assertThat(subSets.size(), equalTo(3)); |
| 67 | + assertThat(lastPartition, equalTo(expectedLastPartition)); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +} |
0 commit comments