Java: How to Split an Arraylist in Multiple Small Arraylists

Java: how can I split an ArrayList in multiple small ArrayLists?

You can use subList(int fromIndex, int toIndex) to get a view of a portion of the original list.

From the API:

Returns a view of the portion of this list between the specified 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 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.

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!)

How to split an arraylist into multiple arraylist of equal sizes?

If the lists won't be modified then you don't need to create an ArrayList<String> for each chunk of data as List<E> support the convenience method List<E> subList(int fromIndex, int toIndex) which returns a "slice" of the original list.

This means that idsVideos.subList(0,50) will return a list containing the first 50 elements. Now you just need to take elements 50 by 50 until reaching last which will be just partial if idsVideos.size()%50 != 0.

final int CHUNK_SIZE = 50;

for (int i = 0; i < idsVideos.size(); i += CHUNK_SIZE) {
List<String> chunk = idsVideos.subList(i, Math.min(i + CHUNK_SIZE, idsVideos.size()));
..
}

Java split Arraylist into smaller ArrayLists

Simply use List<String> as the type of crawl_list1.

That's just an application of the general rule of "code against the interface".

There's really no good reason to require the return value of subList to be an ArrayList (and it doesn't make sense as subList returns a view onto the original ArrayList).

If you absolutely need ArrayList objects, then you need to copy the content into new ArrayList objects:

ArrayList<String> crawl_list1 = new ArrayList<String>(tmp.subList(0, start));
ArrayList<String> crawl_list2 = new ArrayList<String>(tmp.subList(start+1, middle));
ArrayList<String> crawl_list3 = new ArrayList<String>(tmp.subList(middle+1, end));

Split ArrayList into Multiple ArrayLists depending on user input

You can use split list function.

private static List<List<Integer>> splitAndReturn(List<Integer> numbers,
int size) {
List<List<Integer>> smallList = new ArrayList<List<Integer>>();
int i = 0;
while (i + size < numbers.size()) {
smallList.add(numbers.subList(i, i + size));
i = i + size;
}
smallList.add(numbers.subList(i, numbers.size()));
return smallList;
}

The function will return an arrayList of arrays with each raw of size size.
So if you need 100 array with size 10, then

splitAndReturn(yourList, 10).subList(0, 100);

will get you the list of arrays.

How to split an arraylist in multiple arraylists of k lenght in Java ? I don't want a view of the new lists, but I want to create them

You can do it as follows:

import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class Main {
public static void main(String[] args) {
List<Integer> bigList = List.of(0, 0, 1, 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 7, 0, 7, 8, 0, 8, 9,
1, 0, 2, 1, 1, 3, 1, 2, 4, 1, 3, 5, 1, 4, 6, 1, 5, 7, 1, 6, 8, 1, 7, 9, 1, 8, 1, 2, 0, 3, 2, 1, 4, 2, 2,
5, 2, 3, 6, 2, 4, 7, 2, 5, 8, 2, 6, 9, 2, 7, 1, 2, 8, 2, 3, 0, 4, 3, 1, 5, 3, 2, 6, 3, 3, 7, 3, 4, 8, 3,
5, 9, 3, 6, 1, 3, 7, 2, 3, 8, 3, 4, 0, 5, 4, 1, 6, 4, 2, 7, 4, 3, 8, 4, 4, 9, 4, 5, 1, 4, 6, 2, 4, 7, 3,
4, 8, 4, 5, 0, 6, 5, 1, 7, 5, 2, 8, 5, 3, 9, 5, 4, 1, 5, 5, 2, 5, 6, 3, 5, 7, 4, 5, 8, 5, 6, 0, 7, 6, 1,
8, 6, 2, 9, 6, 3, 1, 6, 4, 2, 6, 5, 3, 6, 6, 4, 6, 7, 5, 6, 8, 6, 7, 0, 8, 7, 1, 9, 7, 2, 1, 7, 3, 2, 7,
4, 3, 7, 5, 4, 7, 6, 5, 7, 7, 6, 7, 8, 7, 8, 0, 9, 8, 1, 1, 8, 2, 2, 8, 3, 3, 8, 4, 4, 8, 5, 5, 8, 6, 6,
8, 7, 7, 8, 8, 8);

final AtomicInteger counter = new AtomicInteger(0);
Collection<List<Integer>> smallerLists = bigList.stream()
.collect(Collectors.groupingBy(i -> counter.getAndIncrement() / 9)).values();

// Display the smaller lists
smallerLists.stream().forEach(System.out::println);
}
}

Output:

[0, 0, 1, 0, 1, 2, 0, 2, 3]
[0, 3, 4, 0, 4, 5, 0, 5, 6]
[0, 6, 7, 0, 7, 8, 0, 8, 9]
[1, 0, 2, 1, 1, 3, 1, 2, 4]
[1, 3, 5, 1, 4, 6, 1, 5, 7]
[1, 6, 8, 1, 7, 9, 1, 8, 1]
[2, 0, 3, 2, 1, 4, 2, 2, 5]
[2, 3, 6, 2, 4, 7, 2, 5, 8]
[2, 6, 9, 2, 7, 1, 2, 8, 2]
[3, 0, 4, 3, 1, 5, 3, 2, 6]
[3, 3, 7, 3, 4, 8, 3, 5, 9]
[3, 6, 1, 3, 7, 2, 3, 8, 3]
[4, 0, 5, 4, 1, 6, 4, 2, 7]
[4, 3, 8, 4, 4, 9, 4, 5, 1]
[4, 6, 2, 4, 7, 3, 4, 8, 4]
[5, 0, 6, 5, 1, 7, 5, 2, 8]
[5, 3, 9, 5, 4, 1, 5, 5, 2]
[5, 6, 3, 5, 7, 4, 5, 8, 5]
[6, 0, 7, 6, 1, 8, 6, 2, 9]
[6, 3, 1, 6, 4, 2, 6, 5, 3]
[6, 6, 4, 6, 7, 5, 6, 8, 6]
[7, 0, 8, 7, 1, 9, 7, 2, 1]
[7, 3, 2, 7, 4, 3, 7, 5, 4]
[7, 6, 5, 7, 7, 6, 7, 8, 7]
[8, 0, 9, 8, 1, 1, 8, 2, 2]
[8, 3, 3, 8, 4, 4, 8, 5, 5]
[8, 6, 6, 8, 7, 7, 8, 8, 8]

Split an Arraylist into multiple variable length Arraylists

One more solution is ,
we have source list and start index list.Using these two lists generate end index list.Now use start index list and end index list to generate the required final output

split an array list into multiple lists in java

You can try by using HashMap

    private static class MyItemHashMap extends HashMap {  
public Item add(Item item) {
get(item).add(item);
return item;
}

public List get(Item key) {
List list = (List) get(createItemKey((Item) key));
return list == null ? createItemEntry((Item) key) : list;
}

private List createItemEntry(Item item) {
List list = new ArrayList();
put(createItemKey(item), list);
return list;
}

private Object createItemKey(Item item) {
return item.getSplitterProperty();
}
}

public static void main(String[] args) {
MyItemHashMap itemMapped = new MyItemHashMap();
List items = Arrays.asList(new Object[]{new Item("A"), new Item("B"),
new Item("C")});
for (Iterator iter = items.iterator(); iter.hasNext();) {
Item item = (Item) iter.next();
itemMapped.add(item);
}
}

How split arraylist to multiple lists by specific property object Java8 in one line

Use groupingBy to get a Map<AWSRegion,List<Prop>> and then get the values of that Map:

Collection<List<Prop>> groups =
all.stream()
.collect(Collectors.groupingBy(Prop::getRegion))
.values();

If the output should be a List, add an extra step:

List<List<Prop>> groups = new ArrayList<>(
all.stream()
.collect(Collectors.groupingBy(Prop::getRegion))
.values());


Related Topics



Leave a reply



Submit