Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can use stream API for divide the array into sub array

I have an array of size 1000. I want to use the stream operations to performe like this :-

List list= new ArrayList();
//list is initialize to 1000 elements 

  List subList = list.subList(0, 100);
   // perform some operaions over the subarray
  List subList1 = list.subList(101, 200);
   // perform some operaions over the subarray
 .... so on
}

I want code using stream API. Thanks in advance

like image 466
Hasnain Ali Bohra Avatar asked Feb 16 '26 05:02

Hasnain Ali Bohra


1 Answers

What about :

  List<List<Integer>> result = IntStream.range(0, list.size() / 100)
         .mapToObj(index -> list.subList(index * 100, index * 100 + 100))
         .collect(Collectors.toList());
like image 143
YCF_L Avatar answered Feb 17 '26 19:02

YCF_L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!