Given the code fragment:
What is the result?
Given the code fragment:
What is the result?
The code makes use of Java Streams to evaluate various conditions on the specialDays list and outputs the results. The first line of output comes from the allMatch method, which checks if all elements in the stream match the given predicate (equals 'Labour'). This returns false because not all elements in the list are 'Labour'. The second line uses anyMatch to check if any element in the stream matches 'Labour', which is true. The third line uses noneMatch to check if none of the elements match 'Halloween', which is true as 'Halloween' is not in the list. Finally, findFirst returns an Optional describing the first element of the stream, which is 'NewYear'. Therefore, the output is 'false true true Optional[NewYear]'.
B is the correct answer
B is right answer
*Match returns boolean findFirst returns Optional Result: B is correct
package q08; import java.util.List; public class Q08 { public static void main(String[] args) { List<String> specialDays = List.of("NewYear", "Valentines", "Spring", "Labour"); System.out.print(specialDays.stream().allMatch(s -> s.equals("Labour"))); System.out.print(" " + specialDays.stream().anyMatch(s -> s.equals("Labour"))); System.out.print(" " + specialDays.stream().noneMatch(s -> s.equals("Halloween"))); System.out.print(" " + specialDays.stream().findFirst()); } } // Result: // false true true Optional[NewYear]
B is correct