Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 partition list

Tags:

Is it possible to partition a List in pure Jdk8 into equal chunks (sublists).

I know it is possible using Guava Lists class, but can we do it with pure Jdk? I don't want to add new jars to my project, just for one use case.

SOLUTONS:

The best solution till now was presented by tagir-valeev:

I have also found three other possibilities, but they are ment for only few cases:

1.Collectors.partitioningBy() to split the list into 2 sublists – as follows:

intList.stream().collect(Collectors.partitioningBy(s -> s > 6));     List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values()); 

2.Collectors.groupingBy() to split our list to multiple partitions:

 Map<Integer, List<Integer>> groups =        intList.stream().collect(Collectors.groupingBy(s -> (s - 1) / 3));     List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values()); 

3.Split by separator:

List<Integer> intList = Lists.newArrayList(1, 2, 3, 0, 4, 5, 6, 0, 7, 8);      int[] indexes =        Stream.of(IntStream.of(-1), IntStream.range(0, intList.size())       .filter(i -> intList.get(i) == 0), IntStream.of(intList.size()))       .flatMapToInt(s -> s).toArray();     List<List<Integer>> subSets =        IntStream.range(0, indexes.length - 1)                .mapToObj(i -> intList.subList(indexes[i] + 1, indexes[i + 1]))                .collect(Collectors.toList()); 

4.Using Streams + counter source:

final List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7); final int chunkSize = 3; final AtomicInteger counter = new AtomicInteger();  final Collection<List<Integer>> result = numbers.stream()     .collect(Collectors.groupingBy(it -> counter.getAndIncrement() / chunkSize))     .values(); 
like image 370
Beri Avatar asked Jun 23 '15 06:06

Beri


People also ask

Can you split a list in Java?

In the Guava library, we can use Lists. partition() method that splits the list into consecutive sublists, every list specified size. In order to split the list into two sublists, In our case, we can pass the size that is equal to half the size of our list.

What are partitions in Java?

java. In number theory, * a partition of N is a way to write it as a sum of positive integers. * Two sums that differ only in the order of their terms are considered * the same partition.


2 Answers

That can be done easily using the subList() method:

List<String> collection = new ArrayList<>(21); // fill collection int chunkSize = 10; List<List<String>> lists = new ArrayList<>(); for (int i = 0; i < collection.size(); i += chunkSize) {     int end = Math.min(collection.size(), i + chunkSize);     lists.add(collection.subList(i, end)); } 
like image 149
Robert Avatar answered Oct 01 '22 13:10

Robert


Try using this code, it uses Java 8:

public static Collection<List<Integer>> splitListBySize(List<Integer> intList, int size) {      if (!intList.isEmpty() && size > 0) {         final AtomicInteger counter = new AtomicInteger(0);         return intList.stream().collect(Collectors.groupingBy(it -> counter.getAndIncrement() / size)).values();     }     return null; } 
like image 40
chabbi aissa Avatar answered Oct 01 '22 14:10

chabbi aissa