1

What I've done in the following examples is switch the order of map and limit functions. What I am not able to understand is why Inside Map is getting printed only 3 times in the first case as limit is invoked after map completes execution? It should print 6 times in the first case.

List<String> names=Arrays.asList("Amanda","Akon","Anthony","Jack","John","Jill");

List<String> filteredNames3=names.stream().map((str)->{
            System.out.println("Inside Map");
            return str.substring(0,2);
        }).limit(3).collect(Collectors.toList());
System.out.println("filteredNames3 "+filteredNames3);
        
List<String> filteredNames4=names.stream().limit(3).map((str)->{
            System.out.println("Inside Map");
            return str.substring(0,2);
        }).collect(Collectors.toList());
System.out.println("filteredNames4 "+filteredNames4);
Output->

Inside Map
Inside Map
Inside Map
filteredNames3 [Am, Ak, An]
Inside Map
Inside Map
Inside Map
filteredNames4 [Am, Ak, An]

Edit- In case of skip , Inside Map gets printed 6 times.

List<String>names=Arrays.asList("Amar","Akbar","Anthony","Jack","John","Jill");
List<String> filteredNames1=names.stream().map((str)->{
            System.out.println("Inside Map");
            return str.substring(0,2);
        }).skip(2).collect(Collectors.toList());
System.out.println("filteredNames1 "+filteredNames1);
List<String> filteredNames2=names.stream().skip(2).map((str)->{
            System.out.println("Inside Map");
            return str.substring(0,2);
        }).collect(Collectors.toList());
System.out.println("filteredNames2 "+filteredNames2);
Output-
Inside Map
Inside Map
Inside Map
Inside Map
Inside Map
Inside Map
filteredNames1 [An, Ja, Jo, Ji]
Inside Map
Inside Map
Inside Map
Inside Map
filteredNames2 [An, Ja, Jo, Ji]
  • Because the stream knows it will only process three elements at most, so it only processes up to three elements. – Slaw Mar 09 '21 at 04:09
  • I have a question here, on replacing limit with skip(2). Case 1 prints Inside Map 6 times and Case 2 -> 4 times. Why doesn't the stream know then , that it has to skip 2 elements? – Christian Alderson Mar 09 '21 at 04:15
  • Most likely because the implementation has not, for whatever reason(s), been optimized for that scenario. – Slaw Mar 09 '21 at 04:20
  • Hmm, seems a possible explanation – Christian Alderson Mar 09 '21 at 04:22
  • This may provide more information: https://stackoverflow.com/questions/32414088/java-8-stream-difference-between-limit-and-skip. And I can't guarantee this, but I think there are certain situations where the implementation _could_ optimize `skip`; however, perhaps such an implementation is significantly non-trivial or otherwise just not worth the development time. – Slaw Mar 09 '21 at 04:25
  • what do you think `map()` is doing in your examples?? In short, `map()` is just used to transform one object into another by applying a function. The main deal in your example is `filter()`. – Vishwa Ratna Mar 09 '21 at 04:28
  • @VishwaRatna I'm not seeing where the OP uses a `filter` operation? – Slaw Mar 09 '21 at 04:30
  • @VishwaRatna I have not used the filter operation anywhere – Christian Alderson Mar 09 '21 at 04:32
  • @Slaw The link you provided deals with a similar problem – Christian Alderson Mar 09 '21 at 04:34
  • @ChristianAlderson , my bad. I mean `limit()`. – Vishwa Ratna Mar 09 '21 at 04:38
  • @VishwaRatna Any good resources for learning Spring? – Christian Alderson Mar 09 '21 at 04:40
  • 2
    `limit` is not “invoked after map completes execution”. It is invoked *before `collect`*, the actual operation that says what to do. Remove the `collect` and nothing will happen at all. – Holger Mar 09 '21 at 16:36

0 Answers0