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]