Given the code fragment:
List<String> colors = Arrays.asList(“red”, “green”, “yellow”);
Predicate<String> test = n – > {
System.out.println(“Searching…”);
return n.contains(“red”);
};
colors.stream()
.filter(c -> c.length() > 3)
.allMatch(test);
What is the result?
A.
Searching…
B.
Searching…
Searching…
C.
Searching…
Searching…
Searching…
D.
A compilation error occurs.
A
Please explain how did you get A. Thank you.
D
compilation error . code does not compile.
anybody else with same answer?
Sorry for previous answers.
The correct answer is. A. Searching…..
Bcoz allMatch is Terminal Short Circuiting operator. All it required is one false
https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
// Hello, run this code true debugger.
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
// Given the code fragment:
public class Q2 {
public static void main(String[] args) {
List colors = Arrays.asList(“red”, “green”, “yellow”);
Predicate test = n -> {
System.out.println(“Searching…”);
return n.contains(“red”);
};
colors.stream()
.filter(c -> c.length() > 3)
.allMatch(test);
}
}
/*
What is the result?
A.
Searching…
B.
Searching…
Searching…
C.
Searching…
Searching…
Searching…
D.
A compilation error occurs.
http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html
*/
A
A. is Correct
Does any body have the working code of all these questions