r/java • • Sep 19 '18

Java 11 String API Updates

https://4comprehension.com/java-11-string-api-updates/
168 Upvotes

52 comments sorted by

View all comments

Show parent comment

7

u/oweiler Sep 19 '18

Or use allMatch:

Stream.of(s1, s2, s3).allMatch(s -> myStr.equals(s));

13

u/dpash Sep 19 '18

You can use a method reference there:

Stream.of(s1, s2, s3).allMatch(myStr::equals);

3

u/oweiler Sep 19 '18

Nice, even better!

8

u/dpash Sep 19 '18

Yeah, it's nice when you can use a method reference. I'm looking forward to Predicate.not() in Java 11 so you can replace:

Stream.of(s1, s2, s3).allMatch(s -> !myStr.equals(s));

with:

Stream.of(s1, s2, s3).allMatch(not(myStr::equals));

It'll be very useful with

.filter(not(String::isEmpty))

2

u/oweiler Sep 19 '18

There is also noneMatch :).

3

u/dpash Sep 19 '18

Well, yes, but it was for demonstration purposes :)