Moving to Java 11: Rediscover Some Gems You Might Have Missed
Catch up on the API enhancements you might have missed since Java 8.
Join the DZone community and get the full member experience.Join For Free
If you are a Java developer, and unless you've been living under a rock, you should have been affected in some way by the new course Java began in version 9. This version introduced new, cutting-edge features, like Java modularization, and started the new six-month release schedule. The magnitude of these changes is so big that they sparkled numerous debates about their adoption and their actual benefits. Regardless of our position about these topics, we can't deny that each "post 8" version has introduced notable changes and improvements to our beloved platform in a relatively small time frame (only one year!).
As a side effect of such a dense stream of "new and noteworthy," some small changes (like API improvements) might be eclipsed by more prominent new features and eventually overlooked. This article will help you refresh on such changes, and these changes absolutely deserve to be rediscovered and become part of our everyday work with Java.
Base IO classes
OutputStream have been enriched with new methods to make them more intuitive and developer-friendly.
For class the
InputStream, we find:
static InputStream nullInputStream()(since Java 11): the good old /dev/null as input stream.
byte readAllBytes()(since Java 9): returns all bytes from the input stream. This is useful when the input stream has a known and not huge size.
byte readNBytes(int len)(since Java 11): returns up to len bytes of data from the input stream.
long transferTo(OutputStream out)(since Java 9): copy all the input stream content into the given output stream.
For the class
OutputStream, we find:
static OutputStream nullOutputStream()(since Java 11): the good old /dev/null as output stream.
Reader has been expanded with methods:
static Reader nullReader()(since Java 11): the good old /dev/null as reader.
long transferTo(Writer out)(since Java 10): copy all characters from this reader to the given writer.
Writer, we find:
static Writer nullReader()(since Java 11): the good old /dev/null as writer.
Classes Files and Path
Files have beeenhanceded with a set of methods to read/write
String directly from/to
static Path writeString(Path path, CharSequence csq, OpenOption... options)(since Java 11): writes the entire char sequence to the given path.
static String readString(Path path)(since Java 11): reads the entire content of the given path as string.
Path now offers a usefull factory method:
static Path of(String first, String... more)(since Java 11): builds a Path using a single path string or a sequence of them.
String has gone through a massive improvement since Java 8. Java 9 and 11 continued on this path adding the following methods:
IntStream codePoints()(since Java 9): returns a stream of character codes obtained from the current string value.
IntStream chars()(since Java 9): returns a stream of character codes obtained from the current string value.
boolean isBlank()(since Java 11): checks if the string is empty or contains only white spaces.
Stream<String> lines()(since Java 11): returns the current string content as a stream of lines separated by line terminators.
String repeat(int n)(since Java 11): generate a new string, which is the concatenation of this string value repeated n times.
String strip()(since Java 11): generate a new string, which is produced removing all leading and trailing whitespace from the current string.
String stripLeading()(since Java 11): generate a new string, which is produced removing all leading whitespace from the current string.
String stripTrailing()(since Java 11): generate a new string, which is produced removing all trailing whitespace from the current string.
Java 11 finally introduces a nice solution to the long-standing problem of converting a collection into an array. Interface Collection now comes with the method:
- <T> T toArray(IntFunction<T> generator) (since Java 11): convert the current collection to an array using the given generator.
Since Java 10 also List, Map and Set have an interesting utility method to create a copy:
- static <E> List<E> copyOf(Collection<? extends E> coll) (since Java 10): create an unmodifable copy of the given collection
Streams are probably the most significat feature instroduced in Java 8 along with lambdas. Java 9 brought some nice new methods to work with streams:
- static <T> Stream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next) (since Java 9): creates a sequential ordered Stream produced by iteratively invoking the given next function. The stream terminates when
- Stream<T> takeWhile(Predicate<? super T> predicate) (since Java 9): returns stream elements as long as predicate holds true.
- Stream<T> dropWhile(Predicate<? super T> predicate) (since Java 9): skips stream elements as long as predicate holds true, then returns all the following elements.
In this article, I've tried to summarize the most useful API enhancements introduced since Java 8. Obviously, the list is not exhaustive, but I've tried to consider those changes that should be more relevant in our everyday job. If you think I guiltily left out something, please let me know in the comments below.
Opinions expressed by DZone contributors are their own.