Partition a collection into smaller collections
This article describes how to partition a collections into a given number of smaller collections.
Table of Contents
- 1. Partition a Collection
- 2. Partition Collection in Java
-
- 2.1. Implementation
- 2.2. Test
- 3. About this website
-
- 3.1.
- 4. Links and literature
-
- 4.1. Source Code
- 4.2. General
1. Partition a Collection
Sometime want split a collection into smaller collections. The following gives an example how to does this.
Tip example is a adjusted example of the Google collection and more or less the same as Code Ranch Discussion the Google source code can be found here Original source code
The test code also demonstrate how to calculate the number of elements which should go into one buckets in case you want to Has a fixed number of buckets.
2. Partition collection in Java2.1. Implementation
Create a Java project "De.vogella.algorithms.partitioncollection".
Create the following program.
Package De.vogella.algorithms.partitioncollection;import Java.util.abstractlist;import Java.util.List;public class mypartition {/** * Returns consecutive {@linkplain list#sublist (int, int) sublists} of a List, * each of the same size (th E final list may be smaller). For example, * Partitioning a list containing {@code [A, B, C, D, E]} with a partition * size of 3 yields {@code [[A, B, c ], [D, E]]}--an outer list containing * Double inner lists of three and both elements, all in the original order. * * <p>the outer list is unmodifiable and reflects the latest state of the * source list. The inner lists is sublist views of the original list, * produced on demand using {@link list#sublist (int, int)}, and is Subject * to all the usual caveats about modification as explained on that API. * * * Adapted from http://code.google.com/p/google-collections/* * @param list the list to return consecutive sublists of * @param size the desired size of each sublist (the last could be * smaller) * @return A list of consecutive sublists * @throws illegalargumentexception if {@code partitionsize} is nonpositive * */ public static <T> list<list<t>> partition (list<t> list, int size) {if (list = = null) throw new NullPointerException ("' list ' must not is null"); if (! ( Size > 0)) throw new IllegalArgumentException ("' size ' must be greater than 0"); return new partition<t> (list, size); } private static Class Partition<t> extends Abstractlist<list<t>> {final list<t> List; final int size; Partition (list<t> list, int size) {this.list = list; this.size = size; } @Override public list<t> get (int index) {int listsize = size (); if (Listsize < 0) throw new illegalargumentexception ("Negative size:" + listsize); if (Index < 0) throw new indexoutofboundsexception ("index" + index + "must not is" Negative "); if (index >= listsize) throw new Indexoutofboundsexception ("index" + index + "must is less than size" + lis Tsize); int start = index * size; int end = Math.min (start + size, list.size ()); Return list.sublist (start, end); } @Override public int size () {return (List.size () + size-1)/size; } @Override public Boolean isEmpty () {return list.isempty (); } }}
2.2. Test
You can use the following JUnit test to validate the result.
Package De.vogella.algorithms.partitioncollection;import Java.util.arraylist;import Java.util.list;import Org.junit.test;import static Org.junit.assert.asserttrue;public class Mypartitiontest {@Test public void partitiontest 1 () {list<string> List = new arraylist<string> (); List.add ("one"); List.add ("both"); List.add ("three"); List.add ("four"); List.add ("five"); List.add ("six"); List.add ("seven"); List.add ("eight"); List.add ("Nine"); List.add ("ten"); List.add ("eleven"); list<list<string>> partition = Mypartition.partition (List, 1); System.out.println (Partition.get (2). Size ()); Asserttrue (Partition.size () ==11); Asserttrue (Partition.get (0). Size () ==1); Partition = Mypartition.partition (list, 7); Asserttrue (Partition.size () ==2); Asserttrue (Partition.get (0). Size () ==7); Asserttrue (Partition.get (1). Size () ==4); Now let assume your want to has X number of buckets//How many elements must placed In a list? Take x as 3 int buckets = 3; int divide = list.size ()/buckets; if (list.size ()% buckets!=0) {divide + +; } System.out.println ("Max. Number of element in each bucket "+ divide); Partition = Mypartition.partition (list, divide); Asserttrue (Partition.size () ==buckets); }}
Split/partition a collection into smaller collections-java-tutorial