Hasnain Ali Bohra
Hasnain Ali Bohra

Reputation: 2180

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

Upvotes: 3

Views: 2393

Answers (4)

amaidment
amaidment

Reputation: 7268

To use the Stream API with an array, you want to use StreamSupport and a Spliterator.

Arrays provides utility methods to create a Spliterator.

For example:

int[] array = new int[1000];
StreamSupport.stream(Arrays.spliterator(array, 0, 100), false)
  .forEach(e -> {});
StreamSupport.stream(Arrays.spliterator(array, 100, 200), false)
  .forEach(e -> {});

Note - it's zero-based indexing, and the start index is inclusive and the end index is exclusive.

Upvotes: 1

ItFreak
ItFreak

Reputation: 2369

You could either use Collectors.partitioningBy:

Map<Boolean, List<Integer>> map = list.stream().collect(Collectors.partitioningBy(element -> list.indexOf(element) >= 100));
and then do:  
List<List<Integer>> results = new ArrayList(map.values());

Update: Collectors.partitioningBy takes a predicate and thus is not able to solve the desired use case.

Or if you want to split a list into equal parts (what I think is more youre use case), you could use Collectors.groupingBy():

Map<Integer, List<Integer>> groups = 
      list.stream().collect(Collectors.groupingBy(element -> (element - 1) / YOUR_NUMBER_OF_PIECES_PER_SUBLIST));
    List<List<Integer>> subLists= new ArrayList<List<Integer>>(groups.values());
System.out.println("Number of sublists " + subLists.size());

This gives you:

Number of sublists: 5

when running with NUMBER_OF_PIECES_PER_SUBLIST = 200, which seems your use case.

Upvotes: 2

Samuel Philipp
Samuel Philipp

Reputation: 11042

You can use IntStream.iterate() to achieve that:

int sublistItems = 100;
List<List<Integer>> result = IntStream.iterate(0, i -> i + sublistItems)
        .limit((list.size() + sublistItems - 1) / sublistItems)
        .mapToObj(startIndex -> list.subList(startIndex, Math.min(startIndex + sublistItems, list.size())))
        .collect(Collectors.toList());

If you are using Java 9 or above you can simplify it like that:

int sublistItems = 100;
List<List<Integer>> result = IntStream.iterate(0, i -> i < list.size(), i -> i + sublistItems)
        .mapToObj(startIndex -> list.subList(startIndex, Math.min(startIndex + sublistItems, list.size())))
        .collect(Collectors.toList());

Upvotes: 0

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59960

What about :

  List<List<Integer>> result = IntStream.range(0, list.size() / 100)
         .mapToObj(index -> list.subList(index * 100, index * 100 + 100))
         .collect(Collectors.toList());

Upvotes: 5

Related Questions