What am I doing wrong with java 8 lambda Predicate <Integer>?
This is not a duplicate of my question. I checked it, and mine - use the correct Predicate, and THAT - about the differences between removeIf and removal.
I am starting a Java programmer.
Yesterday I tried to follow this tutorial https://dzone.com/articles/why-we-need-lambda-expressions
After I got rid of using lambda expressions and Predicate, I made my own code for practice.
for example, sum all numbers if (n% 3 == 0 || n% 5 == 0). here is my code.
public class Euler1Lambda {
long max;
public Euler1Lambda(long max) {
this.max = max;
}
public static boolean div3remainder0(int number) {
return number % 3 == 0;
}
public static boolean div5remainder0(int number) {
return number % 5 == 0;
}
public long sumAll() {
long sum = 0;
for(int i=1; i<max; i++) {
if (div3remainder0(i) ||div5remainder0(i)) {
sum += i;
}
}
return sum;
}
public long sumAllLambda(Predicate<Integer> p) {
long total = 0;
for (int i = 1; i< max; i++){
if (p.test(i)) {
total += i;
}
}
return total;
}
public static void main(String[] args) {
//conv
long startTime = System.currentTimeMillis();
for(int i = 0; i < 10; i++){
new Euler1Lambda(100000000).sumAll();
}
long endTime = System.currentTimeMillis();
long conv = (endTime - startTime);
System.out.println("Total execution time: " + conv);
//lambda
startTime = System.currentTimeMillis();
for(int i = 0; i < 10; i++){
new Euler1Lambda(100000000).sumAllLambda(n -> div3remainder0(n) || div5remainder0(n));
}
endTime = System.currentTimeMillis();
long lambda = (endTime - startTime);
System.out.println("Total execution time: " + lambda);
System.out.println("lambda / conv : " + (float)lambda/conv);
}
}
This code performed temporary tests. and the result is this.
Total execution time conv: 1761
Total execution time lambda: 3266
lambda / conv : 1.8546281
, - , for-loop.
, .
? ?
, . 1505 100000000 15 . .
, int Integers Predicate<Integer>. Predicate::test Integer, p.test(i) p.test(Integer.valueOf(i)). , . -, 15 .
IntPredicate, int , , - , - .
, Java ( , , JMH ..). , , , .