Search

Other miscellaneous additions to java.lang, java.util, and elsewhere - java 8 features

There are lot of other additions to other packages we have not yet mentioned. Here are a few of the notable ones. ThreadLocal.withInitial(Supplier) allows for a more compact thread-local variable declaration. The long overdue StringJoiner and String.join(...) are now part of Java 8. Comparator provides some new methods for doing chained and field-based comparisons. The default String pool map size is bigger at around 25-50K.

There is too much there to talk about, but I'll pick out a few noteworthy items.
ThreadLocal.withInitial(Supplier<T>) makes declaring thread-local variables with initial values much nicer. Previously you would supply an initial value like this:
ThreadLocal<List<String>> strings =
    new ThreadLocal<List<String>>() {
        @Override
        protected List<String> initialValue() {
             return new ArrayList<>();
        }
    };
Now it's like this:
ThreadLocal<List<String>> strings =
    ThreadLocal.withInital(ArrayList::new);
Optional<T> appears in the stream API as the return value for methods like min/max, findFirst/Any, and some forms of reduce. It's used because there might not be any elements in the stream, and it provides a fluent API for handling the "some result" versus "no result" cases. You can provide a default value, throw an exception, or execute some action only if the result exists.
It's very, very similar to Guava's Optional class. It's nothing at all like Option in Scala, nor is it trying to be, and the name similarity there is purely coincidental.
Aside: it's interesting that Java 8's Optional and Guava's Optional ended up being so similar, despite the absurd amount of debate that occurred over its addition to both libraries.
"FYI.... Optional was the cause of possibly the single greatest conflagration on the internal Java libraries discussion lists ever."
"On a purely practical note, the discussions surrounding Optional have exceeded its design budget by several orders of magnitude."
StringJoiner and String.join(...) are long, long overdue. They are so long overdue that the vast majority of Java developers likely have already written or have found utilities for joining strings, but it is nice for the JDK to finally provide this itself. Everyone has encountered situations where joining strings is required, and it is a Good Thing™ that we can now express that through a standard API that every Java developer (eventually) will know.
Comparator provides some very nice new methods for doing chained comparisons and field-based comparisons. For example:
people.sort(
    Comparator.comparing(Person::getLastName)
        .thenComparing(Person::getFirstName)
        .thenComparing(
            Person::getEmailAddress,
            Comparator.nullsLast(CASE_INSENSITIVE_ORDER)));
These additions provide good, readable shorthand for complex sorts. Many of the use cases served by Guava's ComparisonChain and Ordering utility classes are now served by these JDK additions. And for what it's worth, I think the JDK verions read better than the functionally-equivalent versions expressed in Guava-ese.


No comments:

Post a Comment