Java stream unexpected result

Alex :

I have following code:

Stream.of("Java", "Stream", "Test")
      .peek(s -> System.out.print(s + " "))
      .allMatch(s -> s.startsWith("J"));

Why does it print me Java Stream?

Ousmane D. :

allMatch is short-circuiting operation as most of the streams operations are. since allMatch returned early that's why peek is only printing the first two elements.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=444996&siteId=1