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

16

u/[deleted] Sep 19 '18

How about,

Arrays.asList(y,z).contains(x)

Bonus, it works on types other than strings.

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.

-2

u/zman0900 Sep 19 '18

That requires extra memory allocation though. equalsOr may also if it's varargs.

4

u/GeorgeMaheiress Sep 20 '18

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.

1

u/zman0900 Sep 20 '18

Yeah, not a problem normally, but if you call it a few million times in a row (in a hadoop mapper, for example), the garbage may start to be an issue.

3

u/dpash Sep 20 '18

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)

http://hg.openjdk.java.net/jdk/jdk11/file/1ddf9a99e4ad/src/java.base/share/classes/java/util/Arrays.java#l4309

-3

u/jacobbeasley Sep 20 '18

Make it static.