Return to site

JAVA CERTIFICATION QUESTION: Search stream data using the findFirst, findAny, and anyMatch methods

Streams are complicated. With the right approach, they can be very efficient too.

· java,ocp

Given the following code:

List src = List.of("Java 11", "Exam");

Which code fragment determines if the word “Java” is present in the src list in the most computationally efficient way? Choose one.

A.

List res = List.of();

src.stream()

.peek(v -> { if (v.contains("Java")) res.add(v); })

.count();

var a = (res.size() > 0);

B.

var a = src.stream().filter(v -> v.contains("Java")).findAny();

C.

var a = src.stream().anyMatch(v -> v.contains("Java"));

D.

var a = src.stream()

.filter(v -> v.contains("Java"))

.collect(Collectors.toList());

The answer is C.