new list partitioning examples

This commit is contained in:
eugenp 2013-12-19 15:09:11 +02:00
parent eb2c88d90c
commit bf18f6d15d
3 changed files with 107 additions and 2 deletions

View File

@ -16,7 +16,13 @@
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
<!-- web -->
<!-- test scoped -->
@ -110,7 +116,7 @@
<httpclient.version>4.3.1</httpclient.version>
<rest-assured.version>2.1.0</rest-assured.version>
<!-- maven plugins -->
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>

View File

@ -0,0 +1,43 @@
package org.baeldung.java;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import java.util.List;
import org.apache.commons.collections4.ListUtils;
import org.junit.Test;
import com.google.common.collect.Lists;
public class CollectionApachePartitionUnitTest {
// tests - apache common collections
@Test
public final void givenList_whenParitioningIntoNSublists_thenCorrect() {
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
final List<List<Integer>> subSets = ListUtils.partition(intList, 3);
// When
final List<Integer> lastPartition = subSets.get(2);
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
assertThat(subSets.size(), equalTo(3));
assertThat(lastPartition, equalTo(expectedLastPartition));
}
@Test
public final void givenListPartitioned_whenOriginalListIsModified_thenPartitionsChange() {
// Given
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
final List<List<Integer>> subSets = ListUtils.partition(intList, 3);
// When
intList.add(9);
final List<Integer> lastPartition = subSets.get(2);
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8, 9);
assertThat(lastPartition, equalTo(expectedLastPartition));
}
}

View File

@ -0,0 +1,56 @@
package org.baeldung.java;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
public class CollectionGuavaPartitionUnitTest {
// tests - guava
@Test
public final void givenList_whenParitioningIntoNSublists_thenCorrect() {
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
final List<List<Integer>> subSets = Lists.partition(intList, 3);
// When
final List<Integer> lastPartition = subSets.get(2);
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
assertThat(subSets.size(), equalTo(3));
assertThat(lastPartition, equalTo(expectedLastPartition));
}
@Test
public final void givenListPartitioned_whenOriginalListIsModified_thenPartitionsChangeAsWell() {
// Given
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
final List<List<Integer>> subSets = Lists.partition(intList, 3);
// When
intList.add(9);
final List<Integer> lastPartition = subSets.get(2);
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8, 9);
assertThat(lastPartition, equalTo(expectedLastPartition));
}
@Test
public final void givenCollection_whenParitioningIntoNSublists_thenCorrect() {
final Collection<Integer> intCollection = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
final Iterable<List<Integer>> subSets = Iterables.partition(intCollection, 3);
// When
final List<Integer> firstPartition = subSets.iterator().next();
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(1, 2, 3);
assertThat(firstPartition, equalTo(expectedLastPartition));
}
}