r/java • u/pivovarit • Sep 19 '18
Java 11 String API Updates
https://4comprehension.com/java-11-string-api-updates/10
u/frzme Sep 19 '18
We'll still have to rely on external web services for left pad though :(
1
1
u/dpash Sep 20 '18
You joke, but we can now implement this much more concisely in Java 11:
public static String leftpad(String string, int padding) { return " ".repeat(padding) + string; }2
u/frzme Sep 20 '18
public static String leftpad(String string, int padding) { return " ".repeat(Math.max(padding - string.length(), 0)) + string; }
Not that nice. (Also repeated implementations are not nice)
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.
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
6
u/zman0900 Sep 19 '18
Use a switch?
switch (val) { case "a": case "b": // do the thing break; default: break; }1
u/syntastical Sep 19 '18
That works but I feel like it’s not any better than If(x.equals(y) || x.equals(z))
I really just want If(x.equalsOr(y, z))
16
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 ajava.util.Arrays.ArrayListobject in Java 11, which is a thinner wrapper around an array thanjava.util.ArrayListis. For example,java.util.ArrayListcaches the list size)-3
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);
1
u/thomascgalvin Sep 20 '18
These are really nice. My only complaint is that common utils' isBlank(string) allows you to check for null in addition to zero-length or all-whitespace strings, which is really helpful.
5
3
u/dpash Sep 20 '18
You'd need a static method for that to work.
String.isBlank(string)vs
string.isBlank()
55
u/TheRedmanCometh Sep 19 '18
Holy shit isBlank should have existed long ago. Strip though is just replace without parameters