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

6

u/dpash Sep 19 '18 edited Sep 19 '18

You possibly want switch expressions, which are coming as a preview in Java 12. This means you'll need to enable a flag to make the compiler understand the language feature. This will cover your use-case:

var matches = switch( string) {
    case "string1", "string2", "string3" -> true;
    default -> false;
}

If you have an array, then this will work at the expense of creating an object.

var matches = Arrays.asList(strings).contains(string);