如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
当前回答
你可以使用Eclipse Collections中的chunk方法:
ArrayList<Integer> list = new ArrayList<>(Interval.oneTo(1000));
RichIterable<RichIterable<Integer>> chunks = Iterate.chunk(list, 10);
Verify.assertSize(100, chunks);
这篇DZone文章中还包含了一些块方法的示例。
注意:我是Eclipse Collections的提交者。
其他回答
Apache Commons Collections 4在ListUtils类中有一个分区方法。下面是它的工作原理:
import org.apache.commons.collections4.ListUtils;
...
int targetSize = 100;
List<Integer> largeList = ...
List<List<Integer>> output = ListUtils.partition(largeList, targetSize);
这对我很有用
/**
* Returns List of the List argument passed to this function with size = chunkSize
*
* @param largeList input list to be portioned
* @param chunkSize maximum size of each partition
* @param <T> Generic type of the List
* @return A list of Lists which is portioned from the original list
*/
public static <T> List<List<T>> chunkList(List<T> list, int chunkSize) {
if (chunkSize <= 0) {
throw new IllegalArgumentException("Invalid chunk size: " + chunkSize);
}
List<List<T>> chunkList = new ArrayList<>(list.size() / chunkSize);
for (int i = 0; i < list.size(); i += chunkSize) {
chunkList.add(list.subList(i, i + chunkSize >= list.size() ? list.size()-1 : i + chunkSize));
}
return chunkList;
}
例如:
List<Integer> stringList = new ArrayList<>();
stringList.add(0);
stringList.add(1);
stringList.add(2);
stringList.add(3);
stringList.add(4);
stringList.add(5);
stringList.add(6);
stringList.add(7);
stringList.add(8);
stringList.add(9);
List<List<Integer>> chunkList = getChunkList1(stringList, 2);
polygenelubricants提供的答案将基于给定数组的大小。我正在寻找将数组分割成给定数量的部分的代码。以下是我对代码所做的修改:
public static <T>List<List<T>> chopIntoParts( final List<T> ls, final int iParts )
{
final List<List<T>> lsParts = new ArrayList<List<T>>();
final int iChunkSize = ls.size() / iParts;
int iLeftOver = ls.size() % iParts;
int iTake = iChunkSize;
for( int i = 0, iT = ls.size(); i < iT; i += iTake )
{
if( iLeftOver > 0 )
{
iLeftOver--;
iTake = iChunkSize + 1;
}
else
{
iTake = iChunkSize;
}
lsParts.add( new ArrayList<T>( ls.subList( i, Math.min( iT, i + iTake ) ) ) );
}
return lsParts;
}
希望它能帮助到别人。
Java8流,一个表达式,没有其他库:
List<String> input = ...
int partitionSize = ...
Collection<List<String>> partitionedList = IntStream.range(0, input.size())
.boxed()
.collect(Collectors.groupingBy(partition -> (partition / partitionSize), Collectors.mapping(elementIndex -> input.get(elementIndex), Collectors.toList())))
.values();
测试:
List<String> input = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h" ,"i");
partitionSize = 1 - > [[a], [b], [c], [d], [e], [f], [g], [h],[我]] partitionSize = 2 - > [[a, b], c, d, e, f, g, h,[我]] partitionSize = 3 - > [a, b, c, d, e, f, g, h,我]] partitionSize = 7 - > [[a, b, c, d, e, f, g], [h,我]] partitionSize = 100 -> [[a, b, c, d, e, f, g, h, i]]
你可以使用Eclipse Collections中的chunk方法:
ArrayList<Integer> list = new ArrayList<>(Interval.oneTo(1000));
RichIterable<RichIterable<Integer>> chunks = Iterate.chunk(list, 10);
Verify.assertSize(100, chunks);
这篇DZone文章中还包含了一些块方法的示例。
注意:我是Eclipse Collections的提交者。