How can I split an ArrayList (size=1000) in multiple ArrayLists of the same size (=10) ?
如何在相同大小(=10)的多个数组列表中分割一个ArrayList (size=1000) ?
ArrayList<Integer> results;
12 个解决方案
#1
259
You can use subList(int fromIndex, int toIndex)
to get a view of a portion of the original list.
您可以使用子列表(int fromIndex, int toIndex)获取原始列表的一部分的视图。
From the API:
从API:
Returns a view of the portion of this list between the specified
fromIndex
, inclusive, andtoIndex
, exclusive. (IffromIndex
andtoIndex
are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.返回该列表中指定的fromIndex(包含)和toIndex(独占)之间的部分的视图。(如果fromIndex和toIndex相等,则返回的列表为空。)返回的列表是由这个列表支持的,所以返回列表中的非结构更改反映在这个列表中,反之亦然。返回的列表支持这个列表支持的所有可选列表操作。
Example:
例子:
List<Integer> numbers = new ArrayList<Integer>(
Arrays.asList(5,3,1,2,9,5,0,7)
);
List<Integer> head = numbers.subList(0, 4);
List<Integer> tail = numbers.subList(4, 8);
System.out.println(head); // prints "[5, 3, 1, 2]"
System.out.println(tail); // prints "[9, 5, 0, 7]"
Collections.sort(head);
System.out.println(numbers); // prints "[1, 2, 3, 5, 9, 5, 0, 7]"
tail.add(-1);
System.out.println(numbers); // prints "[1, 2, 3, 5, 9, 5, 0, 7, -1]"
If you need these chopped lists to be NOT a view, then simply create a new List
from the subList
. Here's an example of putting a few of these things together:
如果您需要这些剪切列表不是视图,那么只需从子列表中创建一个新的列表。这里有一个把这些东西放在一起的例子:
// chops a list into non-view sublists of length L
static <T> List<List<T>> chopped(List<T> list, final int L) {
List<List<T>> parts = new ArrayList<List<T>>();
final int N = list.size();
for (int i = 0; i < N; i += L) {
parts.add(new ArrayList<T>(
list.subList(i, Math.min(N, i + L)))
);
}
return parts;
}
List<Integer> numbers = Collections.unmodifiableList(
Arrays.asList(5,3,1,2,9,5,0,7)
);
List<List<Integer>> parts = chopped(numbers, 3);
System.out.println(parts); // prints "[[5, 3, 1], [2, 9, 5], [0, 7]]"
parts.get(0).add(-1);
System.out.println(parts); // prints "[[5, 3, 1, -1], [2, 9, 5], [0, 7]]"
System.out.println(numbers); // prints "[5, 3, 1, 2, 9, 5, 0, 7]" (unmodified!)
#2
160
You can add the Guava library to your project and use the Lists.partition method, e.g.
您可以将番石榴库添加到项目中并使用列表。分区方法,例如。
List<Integer> bigList = ...
List<List<Integer>> smallerLists = Lists.partition(bigList, 10);
#3
43
Apache Commons Collections 4 has a partition method in the ListUtils
class. Here’s how it works:
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);
#4
15
The answer provided by polygenelubricants splits an array based on given size. I was looking for code that would split an array into a given number of parts. Here is the modification I did to the code:
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;
}
Hope it helps someone.
希望它能帮助一些人。
#5
5
This works for me
这适合我
/**
* 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
*/
private <T> List<List<T>> getChunkList(List<T> largeList , int chunkSize) {
List<List<T>> chunkList = new ArrayList<>();
for (int i = 0 ; i < largeList.size() ; i += chunkSize) {
chunkList.add(largeList.subList(i , i + chunkSize >= largeList.size() ? largeList.size() : i + chunkSize));
}
return chunkList;
}
Eg :
例如:
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);
#6
3
I'm guessing that the issue you're having is with naming 100 ArrayLists and populating them. You can create an array of ArrayLists and populate each of those using a loop.
我猜你的问题是命名100个数组列表并填充它们。您可以创建arraylist数组并使用循环填充其中的每个数组。
The simplest (read stupidest) way to do this is like this:
最简单的(读起来最愚蠢的)方法是这样的:
ArrayList results = new ArrayList(1000);
// populate results here
for (int i = 0; i < 1000; i++) {
results.add(i);
}
ArrayList[] resultGroups = new ArrayList[100];
// initialize all your small ArrayList groups
for (int i = 0; i < 100; i++) {
resultGroups[i] = new ArrayList();
}
// put your results into those arrays
for (int i = 0; i < 1000; i++) {
resultGroups[i/10].add(results.get(i));
}
#7
3
A similar question was discussed here, Java: split a List into two sub-Lists?
这里讨论了一个类似的问题,Java:将一个列表分成两个子列表?
Mainly you can use sublist. More details here : subList
主要可以使用子列表。这里有更多的细节:子列表。
Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list...
返回该列表中从fromIndex(包含)到toIndex(排除)的部分的视图。(如果fromIndex和toIndex相等,则返回的列表为空。)返回列表由这个列表支持,因此返回列表中的更改反映在这个列表中,反之亦然。返回的列表支持此列表支持的所有可选列表操作……
#8
1
You can also use FunctionalJava library - there is partition
method for List
. This lib has its own collection types, you can convert them to java collections back and forth.
您还可以使用FunctionalJava库——列表有分区方法。这个库有自己的集合类型,您可以来回地将它们转换为java集合。
import fj.data.List;
java.util.List<String> javaList = Arrays.asList("a", "b", "c", "d" );
List<String> fList = Java.<String>Collection_List().f(javaList);
List<List<String> partitions = fList.partition(2);
#9
1
Create a new list and add sublist view of source list using addAll method to create a new sublist
List newList = new ArrayList(); newList.addAll(sourceList.subList(startIndex, endIndex));
创建一个新的列表,使用addAll方法添加源列表的子列表视图,创建一个新的子列表列表newList = new ArrayList();newList.addAll(sourceList。分表(startIndex endIndex));
#10
1
import org.apache.commons.collections4.ListUtils;
ArrayList<Integer> mainList = .............;
List<List<Integer>> multipleLists = ListUtils.partition(mainList,100);
int i=1;
for (List<Integer> indexedList : multipleLists){
System.out.println("Values in List "+i);
for (Integer value : indexedList)
System.out.println(value);
i++;
}
#11
0
if you don't want to import the apache commons library try this simple code:
如果您不想导入apache commons库,请尝试以下简单代码:
final static int MAX_ELEMENT = 20;
public static void main(final String[] args) {
final List<String> list = new ArrayList<String>();
for (int i = 1; i <= 161; i++) {
list.add(String.valueOf(i));
System.out.print("," + String.valueOf(i));
}
System.out.println("");
System.out.println("### >>> ");
final List<List<String>> result = splitList(list, MAX_ELEMENT);
for (final List<String> entry : result) {
System.out.println("------------------------");
for (final String elm : entry) {
System.out.println(elm);
}
System.out.println("------------------------");
}
}
private static List<List<String>> splitList(final List<String> list, final int maxElement) {
final List<List<String>> result = new ArrayList<List<String>>();
final int div = list.size() / maxElement;
System.out.println(div);
for (int i = 0; i <= div; i++) {
final int startIndex = i * maxElement;
if (startIndex >= list.size()) {
return result;
}
final int endIndex = (i + 1) * maxElement;
if (endIndex < list.size()) {
result.add(list.subList(startIndex, endIndex));
} else {
result.add(list.subList(startIndex, list.size()));
}
}
return result;
}
#12
0
You need to know the chunk size by which you're dividing your list. Say you have a list of 108 entries
and you need a chunk size of 25
. Thus you will end up with 5 lists
:
您需要知道划分列表的块大小。假设您有一个包含108个条目的列表,并且需要一个大小为25的块。这样你就会得到5个列表:
- 4 having
25 entries
each; - 4、参赛作品25项;
- 1 (the fifth) having
8 elements
. - 1(第五个)有8个元素。
Code:
代码:
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for (int i=0; i<108; i++){
list.add(i);
}
int size= list.size();
int j=0;
List< List<Integer> > splittedList = new ArrayList<List<Integer>>() ;
List<Integer> tempList = new ArrayList<Integer>();
for(j=0;j<size;j++){
tempList.add(list.get(j));
if((j+1)%25==0){
// chunk of 25 created and clearing tempList
splittedList.add(tempList);
tempList = null;
//intializing it again for new chunk
tempList = new ArrayList<Integer>();
}
}
if(size%25!=0){
//adding the remaining enteries
splittedList.add(tempList);
}
for (int k=0;k<splittedList.size(); k++){
//(k+1) because we started from k=0
System.out.println("Chunk number: "+(k+1)+" has elements = "+splittedList.get(k).size());
}
}
#1
259
You can use subList(int fromIndex, int toIndex)
to get a view of a portion of the original list.
您可以使用子列表(int fromIndex, int toIndex)获取原始列表的一部分的视图。
From the API:
从API:
Returns a view of the portion of this list between the specified
fromIndex
, inclusive, andtoIndex
, exclusive. (IffromIndex
andtoIndex
are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.返回该列表中指定的fromIndex(包含)和toIndex(独占)之间的部分的视图。(如果fromIndex和toIndex相等,则返回的列表为空。)返回的列表是由这个列表支持的,所以返回列表中的非结构更改反映在这个列表中,反之亦然。返回的列表支持这个列表支持的所有可选列表操作。
Example:
例子:
List<Integer> numbers = new ArrayList<Integer>(
Arrays.asList(5,3,1,2,9,5,0,7)
);
List<Integer> head = numbers.subList(0, 4);
List<Integer> tail = numbers.subList(4, 8);
System.out.println(head); // prints "[5, 3, 1, 2]"
System.out.println(tail); // prints "[9, 5, 0, 7]"
Collections.sort(head);
System.out.println(numbers); // prints "[1, 2, 3, 5, 9, 5, 0, 7]"
tail.add(-1);
System.out.println(numbers); // prints "[1, 2, 3, 5, 9, 5, 0, 7, -1]"
If you need these chopped lists to be NOT a view, then simply create a new List
from the subList
. Here's an example of putting a few of these things together:
如果您需要这些剪切列表不是视图,那么只需从子列表中创建一个新的列表。这里有一个把这些东西放在一起的例子:
// chops a list into non-view sublists of length L
static <T> List<List<T>> chopped(List<T> list, final int L) {
List<List<T>> parts = new ArrayList<List<T>>();
final int N = list.size();
for (int i = 0; i < N; i += L) {
parts.add(new ArrayList<T>(
list.subList(i, Math.min(N, i + L)))
);
}
return parts;
}
List<Integer> numbers = Collections.unmodifiableList(
Arrays.asList(5,3,1,2,9,5,0,7)
);
List<List<Integer>> parts = chopped(numbers, 3);
System.out.println(parts); // prints "[[5, 3, 1], [2, 9, 5], [0, 7]]"
parts.get(0).add(-1);
System.out.println(parts); // prints "[[5, 3, 1, -1], [2, 9, 5], [0, 7]]"
System.out.println(numbers); // prints "[5, 3, 1, 2, 9, 5, 0, 7]" (unmodified!)
#2
160
You can add the Guava library to your project and use the Lists.partition method, e.g.
您可以将番石榴库添加到项目中并使用列表。分区方法,例如。
List<Integer> bigList = ...
List<List<Integer>> smallerLists = Lists.partition(bigList, 10);
#3
43
Apache Commons Collections 4 has a partition method in the ListUtils
class. Here’s how it works:
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);
#4
15
The answer provided by polygenelubricants splits an array based on given size. I was looking for code that would split an array into a given number of parts. Here is the modification I did to the code:
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;
}
Hope it helps someone.
希望它能帮助一些人。
#5
5
This works for me
这适合我
/**
* 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
*/
private <T> List<List<T>> getChunkList(List<T> largeList , int chunkSize) {
List<List<T>> chunkList = new ArrayList<>();
for (int i = 0 ; i < largeList.size() ; i += chunkSize) {
chunkList.add(largeList.subList(i , i + chunkSize >= largeList.size() ? largeList.size() : i + chunkSize));
}
return chunkList;
}
Eg :
例如:
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);
#6
3
I'm guessing that the issue you're having is with naming 100 ArrayLists and populating them. You can create an array of ArrayLists and populate each of those using a loop.
我猜你的问题是命名100个数组列表并填充它们。您可以创建arraylist数组并使用循环填充其中的每个数组。
The simplest (read stupidest) way to do this is like this:
最简单的(读起来最愚蠢的)方法是这样的:
ArrayList results = new ArrayList(1000);
// populate results here
for (int i = 0; i < 1000; i++) {
results.add(i);
}
ArrayList[] resultGroups = new ArrayList[100];
// initialize all your small ArrayList groups
for (int i = 0; i < 100; i++) {
resultGroups[i] = new ArrayList();
}
// put your results into those arrays
for (int i = 0; i < 1000; i++) {
resultGroups[i/10].add(results.get(i));
}
#7
3
A similar question was discussed here, Java: split a List into two sub-Lists?
这里讨论了一个类似的问题,Java:将一个列表分成两个子列表?
Mainly you can use sublist. More details here : subList
主要可以使用子列表。这里有更多的细节:子列表。
Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list...
返回该列表中从fromIndex(包含)到toIndex(排除)的部分的视图。(如果fromIndex和toIndex相等,则返回的列表为空。)返回列表由这个列表支持,因此返回列表中的更改反映在这个列表中,反之亦然。返回的列表支持此列表支持的所有可选列表操作……
#8
1
You can also use FunctionalJava library - there is partition
method for List
. This lib has its own collection types, you can convert them to java collections back and forth.
您还可以使用FunctionalJava库——列表有分区方法。这个库有自己的集合类型,您可以来回地将它们转换为java集合。
import fj.data.List;
java.util.List<String> javaList = Arrays.asList("a", "b", "c", "d" );
List<String> fList = Java.<String>Collection_List().f(javaList);
List<List<String> partitions = fList.partition(2);
#9
1
Create a new list and add sublist view of source list using addAll method to create a new sublist
List newList = new ArrayList(); newList.addAll(sourceList.subList(startIndex, endIndex));
创建一个新的列表,使用addAll方法添加源列表的子列表视图,创建一个新的子列表列表newList = new ArrayList();newList.addAll(sourceList。分表(startIndex endIndex));
#10
1
import org.apache.commons.collections4.ListUtils;
ArrayList<Integer> mainList = .............;
List<List<Integer>> multipleLists = ListUtils.partition(mainList,100);
int i=1;
for (List<Integer> indexedList : multipleLists){
System.out.println("Values in List "+i);
for (Integer value : indexedList)
System.out.println(value);
i++;
}
#11
0
if you don't want to import the apache commons library try this simple code:
如果您不想导入apache commons库,请尝试以下简单代码:
final static int MAX_ELEMENT = 20;
public static void main(final String[] args) {
final List<String> list = new ArrayList<String>();
for (int i = 1; i <= 161; i++) {
list.add(String.valueOf(i));
System.out.print("," + String.valueOf(i));
}
System.out.println("");
System.out.println("### >>> ");
final List<List<String>> result = splitList(list, MAX_ELEMENT);
for (final List<String> entry : result) {
System.out.println("------------------------");
for (final String elm : entry) {
System.out.println(elm);
}
System.out.println("------------------------");
}
}
private static List<List<String>> splitList(final List<String> list, final int maxElement) {
final List<List<String>> result = new ArrayList<List<String>>();
final int div = list.size() / maxElement;
System.out.println(div);
for (int i = 0; i <= div; i++) {
final int startIndex = i * maxElement;
if (startIndex >= list.size()) {
return result;
}
final int endIndex = (i + 1) * maxElement;
if (endIndex < list.size()) {
result.add(list.subList(startIndex, endIndex));
} else {
result.add(list.subList(startIndex, list.size()));
}
}
return result;
}
#12
0
You need to know the chunk size by which you're dividing your list. Say you have a list of 108 entries
and you need a chunk size of 25
. Thus you will end up with 5 lists
:
您需要知道划分列表的块大小。假设您有一个包含108个条目的列表,并且需要一个大小为25的块。这样你就会得到5个列表:
- 4 having
25 entries
each; - 4、参赛作品25项;
- 1 (the fifth) having
8 elements
. - 1(第五个)有8个元素。
Code:
代码:
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for (int i=0; i<108; i++){
list.add(i);
}
int size= list.size();
int j=0;
List< List<Integer> > splittedList = new ArrayList<List<Integer>>() ;
List<Integer> tempList = new ArrayList<Integer>();
for(j=0;j<size;j++){
tempList.add(list.get(j));
if((j+1)%25==0){
// chunk of 25 created and clearing tempList
splittedList.add(tempList);
tempList = null;
//intializing it again for new chunk
tempList = new ArrayList<Integer>();
}
}
if(size%25!=0){
//adding the remaining enteries
splittedList.add(tempList);
}
for (int k=0;k<splittedList.size(); k++){
//(k+1) because we started from k=0
System.out.println("Chunk number: "+(k+1)+" has elements = "+splittedList.get(k).size());
}
}