Every Friday, we’re showing you a couple of nice new tutorial-style Java 8 features, which take advantage of lambda expressions, extension methods, and other great stuff. You’ll find the source code on GitHub.
Java 8 Goodie: SQL ResultSet Streams
Yes, the SQL subject must be dealt with again. Even if last week, we promised an article on concurrency, there is one very important aspect of Java 8 lambdas and interoperability with “legacy” APIs that we need to talk about, first.
Checked Exceptions
Yes. Unfortunately, those beasts from the past still haunt us, more than ever when we’re using Java 8’s lambda expressions. Already before Java 8’s release, there are a couple of Stack Overflow questions related to the subject.
Arrays.stream(dir.listFiles()).forEach(file -> {
try {
System.out.println(file.getCanonicalPath());
}
catch (IOException e) {
throw new RuntimeException(e);
}
// Ouch, my fingers hurt! All this typing!
});
We think it is safe to say:
Java 8 and checked exceptions don’t match.
A workaround is to write your own CheckedConsumer that wraps the checked exception. Such a consumer will be highly reusable, but… Did you think of all the other FunctionalInterfaces? There are quite a few of them in the java.util.function package:
Some of the many types in java.util.function
jOOλ – Fixing lambda in Java 8
While writing this Java 8 blog series, we’ve constantly run into the need to wrap checked exceptions inside lambda expressions. And what do we geeks do when we frequently run into a problem? We fix it! And we have created jOOλ (also jOOL, jOO-Lambda), ASL 2.0 licensed, where we have duplicated pretty much every FunctionalInterface that is available from the JDK to support checked exceptions. Here’s how you would use jOOλ in the above example:
Arrays.stream(dir.listFiles()).forEach(
Unchecked.consumer(file -> {
// Throw all sorts of checked exceptions
// here, we don't care...
System.out.println(file.getCanonicalPath());
})
);
The above example shows how you can simply ignore and pass checked exceptions as RuntimeExceptions. If you actually want to handle them, you can pass an exception handler lambda:
Arrays.stream(dir.listFiles())
.forEach(Unchecked.consumer(
file -> {
System.out.println(file.getCanonicalPath());
},
e -> {
log.info("Log stuff here", e);
throw new MyRuntimeException(e);
}
);
The second example now seems equally verbose, but don’t worry. You will probably reuse that exception handler and fall back to this:
Unfortunately, most efforts in the Java 8 Streams API were made in the area of correctly implementing parallelisable streams. While this is very useful for those of us actually doing parallel computing, for most others better integration with legacy APIs would have been better. One API that seriously deserves some lifting is JDBC, and we’ve blogged about this before. With jOOλ, you can now generate Streams directly from ResultSets or even from PreparedStatements. Here’s how you prepare:
While Java 8’s lambda expressions are awesome, the new Streams API is pretty incomplete. When implementing the above, we had to implement our own ResultSetIterator, and write all this mess to wrap the iterator in a Stream:
// Unfortunately, this method doesn't exist
Stream.generate(
// Supplier, generating new POJOs
() -> {
rs.next();
return new SQLGoodies.Schema(
rs.getString("SCHEMA_NAME"),
rs.getBoolean("IS_DEFAULT")
);
},
// Predicate, terminating the Stream
() -> { !rs.isLast(); }
);
While jOOλ is an acceptable intermediate solution, and the Guava guys are probably already working out how to fix their library, it is really too bad, that Java 8 is lacking such utility functionality.
But we’re complaining on a high level. Next week, as promised, we’ll see a couple of examples related to concurrency, so stay tuned!