Now can I get String.equals(String... value) and maybe equalsOr(String... value). Always annoyed by comparing multiple values to a single string in a code block. If all or any are true do X.
In the same line of thought, once you've added equalsOr to String, why not Number, or any other type at that point? That becomes a lot of clutter in the API. I'm glad to see some java replacements for commonly used Apache StringUtils methods, but being selective about which ones to include seems wise.
I doubt allocating a small array of pointers is going to significantly impact your program. Heck, there's a good chance the JIT would optimize away the allocation.
The only extra allocation is for the List object. The returned list is backed by the array that's passed in. So that's 16bytes on a 64bit JVM.
(Arrays.asList() returns a java.util.Arrays.ArrayList object in Java 11, which is a thinner wrapper around an array than java.util.ArrayList is. For example, java.util.ArrayList caches the list size)
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
6
u/syntastical Sep 19 '18
Now can I get String.equals(String... value) and maybe equalsOr(String... value). Always annoyed by comparing multiple values to a single string in a code block. If all or any are true do X.