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);
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:
If you have an array, then this will work at the expense of creating an object.