如何将列表拆分为给定数量的子列表? [英] how to split a list into a given number of sub-lists?
问题描述
我有一个可能超过 1000
个字符串的列表,但我不知道具体有多少.
I have a list that may be over 1000
strings, however I do not know how many exactly.
在不丢失列表中的任何成员的情况下,将这个list
拆分为较小的列表 的最佳方法是什么?
What is the best way to split this list
into smaller lists without loosing any members of the list?
例如,如果我有一个 1323
成员的列表,我怎样才能最好地将它分成 3 个几乎均匀大小的列表?
For example If I have a list of 1323
members, how can I best split it into 3 almost evenly sized lists?
我已经看到 Guava
和 Commons
通过分区函数拆分列表的方式,但是该函数会将列表拆分为给定大小的块而不是给定数量的组(子列表).
I have seen the Guava
and Commons
way of splitting lists by the partition function, but that function will split the list into given size of chunks and not given number of groups (sub-lists).
推荐答案
Guava 有一个函数 Lists.partition
会为你做这些
Guava has a function Lists.partition
which will do this for you
用法:
Lists.partition(mylist, mylist.size()/3);
这篇关于如何将列表拆分为给定数量的子列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!