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.
- ThreadLocal.withInitial(Supplier)
- String.join(CharSequence, Charsequence...)
- String.join(CharSequence, Iterable)
- Collections.unmodifiableNavigableSet(NavigableSet)
- Collections.unmodifiableNavigableMap(NavigableMap)
- Collections.synchronizedNavigableSet(NavigableSet)
- Collections.synchronizedNavigableMap(NavigableMap)
- Collections.checkedQueue(Queue, Class)
- Collections.checkedNavigableSet(NavigableSet, Class)
- Collections.checkedNavigableMap(NavigableMap, Class,
Class)
- Collections.emptySortedSet()
- Collections.emptyNavigableSet()
- Collections.emptySortedMap()
- Collections.emptyNavigableMap()
- SplittableRandom
- Optional
- OptionalInt
- OptionalLong
- OptionalDouble
- Base64
- StringJoiner
- Spliterator
- Spliterators
- Comparator.naturalOrder()
- Comparator.reverseOrder()
- Comparator.nullsFirst(Comparator)
- Comparator.nullsLast(Comparator)
- Comparator.comparing(Function, Comparator)
- Comparator.comparing(Function)
- Comparator.comparingInt(ToIntFunction)
- Comparator.comparingLong(ToLongFunction)
- Comparator.comparingDouble(ToDoubleFunction)
- Comparator.reversed()
- Comparator.thenComparing(Comparator)
- Comparator.thenComparing(Function, Comparator)
- Comparator.thenComparing(Function)
- Comparator.thenComparingInt(ToIntFunction)
- Comparator.thenComparingLong(ToLongFunction)
- Comparator.thenComparingDouble(ToDoubleFunction)
- The
following come in forms for array = T[], int[], long[], double[]. There
are too many of these to link, so see the Arrays javadocs for more information.
- Arrays.spliterator(array)
- Arrays.spliterator(array,
int, int)
- Arrays.stream(array)
- Arrays.stream(array,
int, int);
- Arrays.setAll(array,
IntFunction)
- Arrays.parallelSetAll(array,
IntFunction)
- Arrays.parallelPrefix(array,
BinaryOperator)
- Arrays.parallelPrefix(array,
int, int, BinaryOperator)
- Math.addExact(int, int)
- Math.addExact(long, long)
- Math.subtractExact(int, int)
- Math.subtractExact(long, long)
- Math.multiplyExact(int, int)
- Math.multiplyExact(long, long)
- Math.negateExact(int)
- Math.negateExact(long)
- Math.decrementExact(int)
- Math.decrementExact(long)
- Math.incrementExact(int)
- Math.incrementExact(long)
- Math.floorDiv(int, int)
- Math.floorDiv(long, long)
- Math.floorMod(int, int)
- Math.floorMod(long, long)
- Math.toIntExact(long)
- Math.nextDown(float)
- Math.nextDown(double)
- Integer.min(int, int)
- Integer.max(int, int)
- Integer.sum(int, int)
- Long.min(long, long)
- Long.max(long, long)
- Long.sum(long, long)
- Double.min(double, double)
- Double.max(double, double)
- Double.sum(double, double)
- Primitive.hashCode(primitive)
(one new static method on each of the primitive wrapper types)
- Boolean.logicalAnd(boolean, boolean)
- Boolean.logicalOr(boolean, boolean)
- Boolean.logicalXor(boolean, boolean)
- Integer.toUnsignedLong(int)
- Integer.toUnsignedString(int)
- Integer.toUnsignedString(int, int)
- Integer.parseUnsignedInt(String)
- Integer.parseUnsignedInt(String, int)
- Integer.compareUnsigned(int, int)
- Long.toUnsignedString(long, int)
- Long.toUnsignedString(long)
- Long.parseUnsignedLong(String, int)
- Long.parseUnsignedLong(String)
- Long.compareUnsigned(long, long)
- Long.divideUnsigned(long, long)
- Long.remainderUnsigned(long, long)
- BigInteger.longValueExact()
- BigInteger.intValueExact()
- BigInteger.shortValueExact()
- BigInteger.byteValueExact()
- Objects.isNull(Object) - useful as a
predicate, e.g. stream.anyMatch(Objects::isNull)
- Objects.nonNull(Object) - useful as a
predicate, e.g. stream.filter(Objects::nonNull)
- Objects.requireNonNull(T, Supplier)
- Random.ints()
- Random.longs()
- Random.doubles()
- SecureRandom.getStrongSecureRandom()
- BitSet.stream()
- IntSummaryStatistics
- LongSummaryStatistics
- DoubleSummaryStatistics
- Pattern.splitAsStream(CharSequence)
- Pattern.asPredicate()
- Process.waitFor(long, TimeUnit)
- Process.destroyForcibly()
- Process.isAlive()
- ZipFile.stream()
- Adler32.update(ByteBuffer)
- CRC32.update(ByteBuffer)
- Miscellaneous
additions to Logger
- Miscellaneous
additions to Locale
- Miscellaneous
additions to ResultSet
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