我只是實現了一個列表分區適應,因爲我無法使用庫。
所以我想在這裏分享我的代碼:
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
public class ListPartitioning<T> implements Iterable<List<T>> {
private final List<T> list;
private final int partitionSize;
public ListPartitioning(List<T> list, int partitionSize) {
if (list == null) {
throw new IllegalArgumentException("list must not be null");
}
if (partitionSize < 1) {
throw new IllegalArgumentException("partitionSize must be 1 or greater");
}
this.list = list;
this.partitionSize = partitionSize;
}
@Override
public Iterator<List<T>> iterator() {
return new ListPartitionIterator<T>(list, partitionSize);
}
private static class ListPartitionIterator<T> implements Iterator<List<T>> {
private int index = 0;
private List<T> listToPartition;
private int partitionSize;
private List<T> nextPartition;
public ListPartitionIterator(List<T> listToPartition, int partitionSize) {
this.listToPartition = listToPartition;
this.partitionSize = partitionSize;
}
@Override
public boolean hasNext() {
return index < listToPartition.size();
}
@Override
public List<T> next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
int partitionStart = index;
int partitionEnd = Math.min(index + partitionSize, listToPartition.size());
nextPartition = listToPartition.subList(partitionStart, partitionEnd);
index = partitionEnd;
return nextPartition;
}
@Override
public void remove() {
if (nextPartition == null) {
throw new IllegalStateException("next must be called first");
}
nextPartition.clear();
index -= partitionSize;
nextPartition = null;
}
}
}
而基於TestNG的單元測試。
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.*;
public class ListPartitioningTest {
@Test(expectedExceptions = IllegalArgumentException.class)
public void nullList() {
ListPartitioning<String> lists = new ListPartitioning<String>(null, 1);
}
@Test(groups = Group.UNIT_TEST, expectedExceptions = IllegalArgumentException.class)
public void wrongPartitionSize() {
ListPartitioning<String> lists = new ListPartitioning<String>(new ArrayList<String>(), 0);
}
@Test()
public void iteratorTest() {
List<Integer> integers = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
ListPartitioning<Integer> listPartitioning = new ListPartitioning<Integer>(integers, 7);
Iterator<List<Integer>> partitionIterator = listPartitioning.iterator();
Assert.assertNotNull(partitionIterator);
Assert.assertTrue(partitionIterator.hasNext(), "next partition (first)");
List<Integer> partition = partitionIterator.next();
Assert.assertEquals(partition, Arrays.asList(0, 1, 2, 3, 4, 5, 6));
Assert.assertTrue(partitionIterator.hasNext(), "next partition (second)");
partition = partitionIterator.next();
Assert.assertEquals(partition, Arrays.asList(7, 8, 9, 10, 11, 12, 13));
Assert.assertTrue(partitionIterator.hasNext(), "next partition (third)");
partition = partitionIterator.next();
Assert.assertEquals(partition, Arrays.asList(14, 15));
Assert.assertFalse(partitionIterator.hasNext());
}
@Test(expectedExceptions = NoSuchElementException.class)
public void noSuchElementException() {
List<Integer> integers = Arrays.asList(1);
ListPartitioning<Integer> listPartitioning = new ListPartitioning<Integer>(integers, 2);
Iterator<List<Integer>> partitionIterator = listPartitioning.iterator();
List<Integer> partition = partitionIterator.next();
partition = partitionIterator.next();
}
@Test(expectedExceptions = IllegalStateException.class)
public void removeWithoutNext() {
List<Integer> integers = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
ListPartitioning<Integer> listPartitioning = new ListPartitioning<Integer>(integers, 7);
Iterator<List<Integer>> partitionIterator = listPartitioning.iterator();
partitionIterator.remove();
}
@Test()
public void remove() {
List<Integer> integers = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
ListPartitioning<Integer> listPartitioning = new ListPartitioning<Integer>(integers, 7);
Iterator<List<Integer>> partitionIterator = listPartitioning.iterator();
partitionIterator.next();
partitionIterator.next();
partitionIterator.remove();
Assert.assertTrue(partitionIterator.hasNext(), "next partition ");
List<Integer> partition = partitionIterator.next();
Assert.assertEquals(partition, Arrays.asList(14, 15));
Assert.assertFalse(partitionIterator.hasNext());
Assert.assertEquals(integers, Arrays.asList(0, 1, 2, 3, 4, 5, 6, 14, 15));
}
}
+1不錯!我需要更好地學習番石榴API ... – alpian 2011-04-28 21:10:23
如果你有一個'Iterable'而不是'List',還有'Iterables.partition(Iterable,int)' – Krease 2015-12-16 00:16:51
最後一個鏈接已經改變了,我想這是一個https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/Lists.html#partition(java.util.List,int) – 2016-12-20 09:44:52