3

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

5
  • you remember that list start with index 0, right? Commented Apr 11, 2019 at 6:21
  • so the ranges are 0-99, 100-199 and so on, right? Commented Apr 11, 2019 at 6:25
  • @SharonBenAsher You might want to merge both questions into one comment. It looks silly right now. (flag my comment for removal so it gets removed when you're done). Commented Apr 11, 2019 at 6:37
  • yes @SharonBenAsher actually for the first sublist the number of element will be from 0 to 100 i.e. 101 elements Commented Apr 11, 2019 at 6:38
  • but .subList(0, 100) has only 100 elements. Whereas .subList(101, 200) will have 99 elements. Commented Apr 11, 2019 at 13:58

4 Answers 4

5

What about :

  List<List<Integer>> result = IntStream.range(0, list.size() / 100)
         .mapToObj(index -> list.subList(index * 100, index * 100 + 100))
         .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

@ltFreak im not sure the partitioningBy will work for my condition as the use case of partitioningBy is to dived the list into two parts based on provided predicate.
@HasnainAliBohra you were right, I extended my answer
1

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.

Comments

0

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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.