I see Let’s Get Lazy: Explore the Real Power of Streams video on youtube (venkat subramaniam). (minute 26-30 approx.)
In the example, a for loop:
List<Integer> numbers = Arrays.asList(1, 2, 3, 5, 4, 6, 7, 8, 9, 10);
int result = 0;
for(int e: values){
if(e > 3 && e % 2 == 0){
result = e * 2;
break;
}
}
having 8 "unit operations"
according to his example :
public class MainClass {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 5, 4, 6, 7, 8, 9, 10);
System.out.println(
numbers.stream()
.filter(e -> e > 3)
.filter(e -> e % 2 == 0)
.map(e -> e * 2)
.findFirst()
.orElse(0)
);
}
}
this code looks like it has 21 "unit operations".
And then he recommends to use this code:
public class MainClass {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 5, 4, 6, 7, 8, 9, 10);
System.out.println(
numbers.stream()
.filter(MainClass::isGT3)
.filter(MainClass::isEven)
.map(MainClass::doubleIt)
.findFirst()
.orElse(0)
);
}
private static int doubleIt(Integer e) {
return e * 2;
}
private static boolean isEven(Integer e) {
return e % 2 == 0;
}
private static boolean isGT3(Integer e) {
return e > 3;
}
}
Really I want to understand, how can this be proved that has 8 unit operations instead of 21 unit operations?