Skip to content

Commit bf18f6d

Browse files
author
eugenp
committed
new list partitioning examples
1 parent eb2c88d commit bf18f6d

3 files changed

Lines changed: 107 additions & 2 deletions

File tree

‎guava/pom.xml‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
<artifactId>guava</artifactId>
1717
<version>15.0</version>
1818
</dependency>
19-
19+
20+
<dependency>
21+
<groupId>org.apache.commons</groupId>
22+
<artifactId>commons-collections4</artifactId>
23+
<version>4.0</version>
24+
</dependency>
25+
2026
<!-- web -->
2127

2228
<!-- test scoped -->
@@ -110,7 +116,7 @@
110116
<httpclient.version>4.3.1</httpclient.version>
111117

112118
<rest-assured.version>2.1.0</rest-assured.version>
113-
119+
114120
<!-- maven plugins -->
115121
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
116122
<maven-war-plugin.version>2.4</maven-war-plugin.version>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.List;
7+
8+
import org.apache.commons.collections4.ListUtils;
9+
import org.junit.Test;
10+
11+
import com.google.common.collect.Lists;
12+
13+
public class CollectionApachePartitionUnitTest {
14+
15+
// tests - apache common collections
16+
17+
@Test
18+
public final void givenList_whenParitioningIntoNSublists_thenCorrect() {
19+
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
20+
21+
final List<List<Integer>> subSets = ListUtils.partition(intList, 3);
22+
23+
// When
24+
final List<Integer> lastPartition = subSets.get(2);
25+
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
26+
assertThat(subSets.size(), equalTo(3));
27+
assertThat(lastPartition, equalTo(expectedLastPartition));
28+
}
29+
30+
@Test
31+
public final void givenListPartitioned_whenOriginalListIsModified_thenPartitionsChange() {
32+
// Given
33+
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
34+
final List<List<Integer>> subSets = ListUtils.partition(intList, 3);
35+
36+
// When
37+
intList.add(9);
38+
final List<Integer> lastPartition = subSets.get(2);
39+
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8, 9);
40+
assertThat(lastPartition, equalTo(expectedLastPartition));
41+
}
42+
43+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.Collection;
7+
import java.util.List;
8+
9+
import org.junit.Test;
10+
11+
import com.google.common.collect.Iterables;
12+
import com.google.common.collect.Lists;
13+
14+
public class CollectionGuavaPartitionUnitTest {
15+
16+
// tests - guava
17+
18+
@Test
19+
public final void givenList_whenParitioningIntoNSublists_thenCorrect() {
20+
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
21+
22+
final List<List<Integer>> subSets = Lists.partition(intList, 3);
23+
24+
// When
25+
final List<Integer> lastPartition = subSets.get(2);
26+
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
27+
assertThat(subSets.size(), equalTo(3));
28+
assertThat(lastPartition, equalTo(expectedLastPartition));
29+
}
30+
31+
@Test
32+
public final void givenListPartitioned_whenOriginalListIsModified_thenPartitionsChangeAsWell() {
33+
// Given
34+
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
35+
final List<List<Integer>> subSets = Lists.partition(intList, 3);
36+
37+
// When
38+
intList.add(9);
39+
final List<Integer> lastPartition = subSets.get(2);
40+
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8, 9);
41+
assertThat(lastPartition, equalTo(expectedLastPartition));
42+
}
43+
44+
@Test
45+
public final void givenCollection_whenParitioningIntoNSublists_thenCorrect() {
46+
final Collection<Integer> intCollection = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
47+
48+
final Iterable<List<Integer>> subSets = Iterables.partition(intCollection, 3);
49+
50+
// When
51+
final List<Integer> firstPartition = subSets.iterator().next();
52+
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(1, 2, 3);
53+
assertThat(firstPartition, equalTo(expectedLastPartition));
54+
}
55+
56+
}

0 commit comments

Comments
 (0)