Java SE 8 For the Really Impatient, Note 12
1,930 words in 12 minutes
Chapter 8 Miscellaneous Goodies
- Joining strings with a delimiter is finally easy:
String.join(",", "a", "b", "c")instead of
a + ", " + b + ", " + c".
- Integer types now support unsigned arithmetic.
Mathclass has methods to detect integer overflow.
Math.floorMod(x, n)instead of
x % nif x might be negative.
- There are a few mutators in
Files.lineslazily reads a stream of lines.
Files.listlazily lists the entries of a directory, and
Files.walktraverses them recursively.
- There is finally official support for Base64 encoding.
- Annotations can now be repeated and applied to type uses.
- Convenient support for null parameter checks can be found in the
A common task is to combine several strings, separating them with a delimiter such as “, “ or “/“. This has now been added to Java 8. The strings can come from an array or an
Iterable<? extends CharSequence>.
join as the opposite of the
String.split instance method.
Since Java 5, each of seven numeric primitive type wrappers(i.e, not
Boolean) had a static
SIZE field that gives the size of the type in bits.
There is now
BYTES field that reports the size in bytes.
All eight primitive type wrappers now have static
hashCode methods that return the same hash code as the instance methods, but without the need for boxing.
The five types
Double now have static methods
min, which can be useful as reduction functions in stream operations.
Boolean class has static
logicalXor for the same purpose.
Integer types now support unsigned arithmetic. E.g., instead of having a
Byte represent the range from -128 to 127, you can call the static method
Byte.toUnsignedInt(b) and get a value between 0 and 255. The
Short classes have methods
Integer have methods
Long classes have methods
remainderUnsigned to work with unsigned values. Integer multiplication would overflow with unsigned integers larger than
Integer.MAX_VALUE, so you should call
toUnsignedLong and multiply them as long values.
Double classes have static methods
isFinite. The call
Double.isFinite(x) returns true if x is not infinity, negative infinity, or a NaN. In the past, you had to call the instance methods
isNaN to get the same result.
BigInteger class has instance methods
(long|int|short|byte)ValueExact that return the values as a long, int, short, or byte, throwing an
ArithmeticException if the value is not within the target range.
New Mathematical Functions
Math class provides several methods for “exact” arithmetic that throw an exception when a result overflows. E.g., 100000 * 100000 quietly gives the wrong result 1410065408, whereas
multiplyExact(100000, 100000) throws an exception. The provided methods are
long parameters. The
toIntExact method converts a
long to the equivalent
floorDiv methods aim to solve a long-standing problem with integer remainders: n % 2 is -1 when n is negative and odd.
floorMod(position + adjustment, 12) always yields a value between 0 and 11.
floorMod gives negative results for negative divisors, but that situation doesn’t often occur in practice.
nextDown method, defined for both
float parameters, gives the next smaller floating-point number for a given number. E.g., if you promise to produce a number < b, but you happen to have computed exactly b, then you can return
Math.nextUp method exists since Java 6.)
All methods described in this section also exist in the
Methods Added to Collection Classes
Methods Added to Collections Classes and Interface in Java 8, other than the stream, parallelStream, and spliterator
|Map||forEach, replace, replaceAll, remove(key, value)(removes only if key mapped to value),
putIfAbsent, compute, computeIf(Absent | Present), merge
removeIf: can be thought of as the opposite of
filter, removing rather than producing all matches and carrying out the removal in place. The
distinct method would be costly to provide on arbitrary collections.
List interface has a
replaceAll method, which is an in-place equivalent of
map, and a
sort method that is obviously useful.
Iterator interface has a
forEachRemaining method that exhausts the iterator by feeding the remaining iterator elements to a function.
BitSet class has a method that yields all members of the set as a stream of int values.
comparing method takes a “key extractor” function that maps a type T to a comparable type (such as String). The function is applied to the objects to be compared, and the comparison is then made on the returned keys.
You can chain comparators with
thenComparing method for breaking ties
If two people have the same last name, then the second comparator is used.
You can specify comparator to be used for the keys that the
thenComparing methods extract.
thenComparing methods have variants that avoid boxing of int, long, or double values.
If your key function can return null, use
nullsLast adapters. These static methods take an existing comparator and modify it so that it doesn’t throw an exception when encountering null values but ranks them as smaller or larger than regular values. Suppose
getMiddleName returns a null when a person has no middle name. Then you can use
naturalOrder method makes a comparator for any class implementing
Comparator.<String>natrualOrder() is what we need.
reverseOrder method gives the reverse of the natural order. To reverse any comparator, use the
reversed instance method.
anturalOrder().reversed() is the same as
The Collections Class
Java 6 introduces
NavigableMap interfaces that take advantage of the ordering of the elements or keys, providing efficient methods to locate, for any given value v, the smallest element >= or > v, or the largest element <= or < v.
Collections class supports these interfaces as it does other collections, with methods
checkedQueue wrapper has also been added. As as reminder, the
checked wrappers have a
Class parameter and throw a
ClassCastException when you insert an element of the wrong type. These classes are intended as debugging aids. Suppose you declare a
Queue<Path>, and somewhere in your code there is a
ClassCastException trying to cast a String to a Path. If you temporarily replace the queue with a
CheckedQueue(new LinkedList<Path>, Path.class), then every insertion is checked at runtime, and you can locate the faulty insertion code.
emptySorted(Set|Map) methods that give lightweight instances of sorted collections, analogous to the