commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chris Shayan (Commented) (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (COLLECTIONS-393) Split / Partition a collection into smaller collections
Date Wed, 29 Feb 2012 04:18:24 GMT

    [ https://issues.apache.org/jira/browse/COLLECTIONS-393?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13218894#comment-13218894
] 

Chris Shayan commented on COLLECTIONS-393:
------------------------------------------

the class is like this:

package dk.sebpension;

import java.util.AbstractList;
import java.util.List;

/**
 * Split / Partition a collection into smaller collections Inspired by Lars
 * Vogel
 */
public final class Partition<T> extends AbstractList<List<T>> {
	final List<T> list;
	final int size;

	public 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 be negative");			
		}
		if (index >= listSize) {
			throw new IndexOutOfBoundsException("index " + index
					+ " must be less than size " + listSize);			
		}			
		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();
	}

	/**
	 * Returns consecutive {@linkplain List#subList(int, int) sublists} of a
	 * list, each of the same size (the 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
	 * two inner lists of three and two elements, all in the original order.
	 * 
	 * <p>
	 * The outer list is unmodifiable, but reflects the latest state of the
	 * source list. The inner lists are sublist views of the original list,
	 * produced on demand using {@link List#subList(int, int)}, and are subject
	 * to all the usual caveats about modification as explained in that API.
	 * 
	 * Adapted from http://code.google.com/p/google-collections/ * @param <T>
	 * 
	 * @param list
	 *            the list to return consecutive sublists of
	 * @param size
	 *            the desired size of each sublist (the last may be smaller)
	 * @return a list of consecutive sublists
	 */
	public static <T> List<List<T>> partition(List<T> list, int size)
{
		if (list == null) {
			throw new NullPointerException("'list' must not be null");			
		}
		if (!(size > 0)) {
			throw new IllegalArgumentException("'size' must be greater than 0");			
		}

		return new Partition<T>(list, size);
	}
}

and example of it is:

			List<List<String>> strings = Partition.partition(raws, 5);
			for (List<String> list : strings) {
				// 
			}

                
> Split / Partition a collection into smaller collections
> -------------------------------------------------------
>
>                 Key: COLLECTIONS-393
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-393
>             Project: Commons Collections
>          Issue Type: New Feature
>          Components: Collection
>            Reporter: Chris Shayan
>         Attachments: Partition.java
>
>   Original Estimate: 24h
>  Remaining Estimate: 24h
>
> Returns consecutive sublists of a list, each of the same size (the final list may be
smaller). For example, partitioning a list containing [a, b, c, d, e] with a partition size
of 3 yields [[a, b, c], [d, e]] -- an outer list containing two inner lists of three and two
elements, all in the original order. 
> The outer list is unmodifiable, but reflects the latest state of the source list. The
inner lists are sublist views of the original list, produced on demand using List.subList(int,
int), and are subject to all the usual caveats about modification as explained in that API.
Adapted from http://code.google.com/p/google-collections/
> Inspired by Lars Vogel

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message