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

Given the following code:
List<String> 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<String> 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.

All Posts
×

Almost done…

We just sent you an email. Please click the link in the email to confirm your subscription!

OK