将列表划分为n个大小的列表的有效方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将列表划分为n个大小的列表的有效方法相关的知识,希望对你有一定的参考价值。
我有一个数组,我想分成更小的n个数组,并对每个数组执行操作。我目前的做法是
用Java中的ArrayLists实现(任何伪代码都可以)
for (int i = 1; i <= Math.floor((A.size() / n)); i++) {
ArrayList temp = subArray(A, ((i * n) - n),
(i * n) - 1);
// do stuff with temp
}
private ArrayList<Comparable> subArray(ArrayList A, int start,
int end) {
ArrayList toReturn = new ArrayList();
for (int i = start; i <= end; i++) {
toReturn.add(A.get(i));
}
return toReturn;
}
其中A是列表,n是所需列表的大小
我相信这种方式在处理相当大的列表(大小高达100万)时花费了太多时间,所以我试图弄清楚什么会更有效率。
你想要做一些利用List.subList(int, int)视图而不是复制每个子列表的东西。要轻松地做到这一点,请使用Guava的Lists.partition(List, int)方法:
List<Foo> foos = ...
for (List<Foo> partition : Lists.partition(foos, n)) {
// do something with partition
}
请注意,与许多事物一样,这不是非常有效的List
不是RandomAccess
(如LinkedList
)。
这是一种将List分区为子列表数组的方法,它确保除最后一个子列表之外的所有子列表都具有相同数量的元素:
static <T> List<T>[] split(List<T> source, int numPartitions) {
if (numPartitions < 2)
return new List[]{source};
final int sourceSize = source.size(),
partitions = numPartitions > sourceSize ? sourceSize: numPartitions,
increments = sourceSize / partitions;
return IntStream.rangeClosed(0, partitions)
.mapToObj(i -> source.subList(i*increments, Math.min((i+1)*increments, sourceSize)))
.toArray(List[]::new);
}
如果你想保证numPartitions
数组大小,那么你想要:
static <T> List<T>[] split(List<T> source, int numPartitions) {
if (numPartitions < 2)
return new List[]{source};
final int sourceSize = source.size(),
partitions = numPartitions > sourceSize ? sourceSize: numPartitions,
increments = sourceSize / partitions;
return IntStream.range(0, partitions)
.mapToObj(i -> source.subList(i*increments, i == partitions-1 ? sourceSize : (i+1)*increments))
.toArray(List[]::new);
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SubListTest
{
public static void main(String[] args)
{
List<String> alphabetNames = new ArrayList<String>();
// populate alphabetNames array with AAA,BBB,CCC,.....
int a = (int) 'A';
for (int i = 0; i < 26; i++)
{
char x = (char) (a + i);
char[] array = new char[3];
Arrays.fill(array, x);
alphabetNames.add(new String(array));
}
int[] maxListSizes = new int[]
{
5, 10, 15, 20, 25, 30
};
for (int maxListSize : maxListSizes)
{
System.out.println("######################################################");
System.out.println("Partitioning original list of size " + alphabetNames.size() + " in to sub lists of max size "
+ maxListSize);
ArrayList<List<String>> subListArray = new ArrayList<List<String>>();
if (alphabetNames.size() <= maxListSize)
{
subListArray.add(alphabetNames);
}
else
{
// based on subLists of maxListSize X
int subListArraySize = (alphabetNames.size() + maxListSize - 1) / maxListSize;
for (int i = 0; i < subListArraySize; i++)
{
subListArray.add(alphabetNames.subList(i * maxListSize,
Math.min((i * maxListSize) + maxListSize, alphabetNames.size())));
}
}
System.out.println("Resulting number of partitions " + subListArray.size());
for (List<String> subList : subListArray)
{
System.out.println(subList);
}
}
}
}
输出:
######################################################
Partitioning original list of size 26 in to sub lists of max size 5
Resulting number of partitions 6
[AAA, BBB, CCC, DDD, EEE]
[FFF, GGG, HHH, III, JJJ]
[KKK, LLL, MMM, NNN, OOO]
[PPP, QQQ, RRR, SSS, TTT]
[UUU, VVV, WWW, XXX, YYY]
[ZZZ]
######################################################
Partitioning original list of size 26 in to sub lists of max size 10
Resulting number of partitions 3
[AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH, III, JJJ]
[KKK, LLL, MMM, NNN, OOO, PPP, QQQ, RRR, SSS, TTT]
[UUU, VVV, WWW, XXX, YYY, ZZZ]
######################################################
Partitioning original list of size 26 in to sub lists of max size 15
Resulting number of partitions 2
[AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH, III, JJJ, KKK, LLL, MMM, NNN, OOO]
[PPP, QQQ, RRR, SSS, TTT, UUU, VVV, WWW, XXX, YYY, ZZZ]
######################################################
Partitioning original list of size 26 in to sub lists of max size 20
Resulting number of partitions 2
[AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH, III, JJJ, KKK, LLL, MMM, NNN, OOO, PPP, QQQ, RRR, SSS, TTT]
[UUU, VVV, WWW, XXX, YYY, ZZZ]
######################################################
Partitioning original list of size 26 in to sub lists of max size 25
Resulting number of partitions 2
[AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH, III, JJJ, KKK, LLL, MMM, NNN, OOO, PPP, QQQ, RRR, SSS, TTT, UUU, VVV, WWW, XXX, YYY]
[ZZZ]
######################################################
Partitioning original list of size 26 in to sub lists of max size 30
Resulting number of partitions 1
[AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH, III, JJJ, KKK, LLL, MMM, NNN, OOO, PPP, QQQ, RRR, SSS, TTT, UUU, VVV, WWW, XXX, YYY, ZZZ]
关于什么
Arrays.copyOfRange( original, from, to )
?
使用Java 8的一行:
IntStream.range(0, list.size() / batchSize + 1)
.mapToObj(i -> list.subList(i * batchSize,
Math.min(i * batchSize + batchSize, list.size())))
.filter(s -> !s.isEmpty()).collect(Collectors.toList());
如果您正在使用列表,我使用“Apache Commons Collections 4”库。它在ListUtils类中有一个分区方法:
...
int targetSize = 100;
List<Integer> largeList = ...
List<List<Integer>> output = ListUtils.partition(largeList, targetSize);
这种方法改编自http://code.google.com/p/guava-libraries/
例如:
int partitionSize = 10;
List<List<String>> partitions = new ArrayList<>();
for (int i=0; i<yourlist.size(); i += partitionSize) {
partitions.add(yourlist.subList(i, Math.min(i + partitionSize, yourlist.size())));
}
for (List<String> list : partitions) {
//Do your stuff on each sub list
}
好吧,在我看到ColinD的回答(+1)之前我自己写了一篇,使用Guava绝对是要走的路。单独留下太有趣了,所以下面给出了列表的副本而不是视图,因此GUava的效率肯定比这更高。我发布这个是因为写它很有趣而不是暗示它有效:
Hamcrest测试(无论如何):
assertThat(chunk(asList("a", "b", "c", "d", "e"), 2),
equalTo(asList(asList("a", "b"), asList("c", "d"), asList("e"))));
代码:
public static <T> Iterable<Iterable<T>> chunk(Iterable<T> in, int size) {
List<Iterable<T>> lists = newArrayList();
Iterator<T> i = in.iterator();
while (i.hasNext()) {
List<T> list = newArrayList();
for (int j=0; i.hasNext() && j<size; j++) {
list.add(i.next());
}
lists.add(list);
}
return lists;
}
以上是关于将列表划分为n个大小的列表的有效方法的主要内容,如果未能解决你的问题,请参考以下文章 2021-12-24:划分字母区间。 字符串 S 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。返回一个表示每个字符串片段的长度的列表。 力扣763。某大厂面试