Java 8 Articles
A couple of Articles explaining some of the key features of Java 8.

The Benefits

Java 8 isn't your parent’s Java anymore. It is the largest
update to Java in its history. It's a very exciting time.
Some of the best ideas from functional programming are
migrating their way into Java 8. This means easier to read,
more flexible code that scales to multicore.

At the end of this course, you will be ready to use Java
8 on your day job and be familiar with the cutting edge
programming approaches which allow you to write more
flexible and concise code.

Articles

Unit Testing Lambda Expressions & Streams

Usually, when writing a unit test you call a method in your test code that gets called in
your application. Given some inputs and possibly test doubles, you call these methods
to test a certain behavior happening and then specify the changes you expect to result
from this behavior.

Lambda expressions pose a slightly different challenge when unit testing code. Because
they don’t have a name, it’s impossible to directly call them in your test code.
You could choose to copy the body of the lambda expression into your test and then
test that copy, but this approach has the unfortunate side effect of not actually testing
the behavior of your implementation. If you change the implementation code, your test
will still pass even though the implementation is performing a different task.

There are two viable solutions to this problem. The first is to view the lambda expression
as a block of code within its surrounding method. If you take this approach, you should
be testing the behavior of the surrounding method, not the lambda expression itself.

Here’s an example method for converting a list of strings into their uppercase equivalents.

The only thing that the lambda expression in this body of code does is directly call a
core Java method. It’s really not worth the effort of testing this lambda expression as an
independent unit of code at all, since the behavior is so simple.

If I were to unit test this code, I would focus on the behavior of the method. For example,
here is a test that if there are multiple words in the stream, they are all converted
to their uppercase equivalents.

Sometimes you want to use a lambda expression that exhibits complex functionality.
Perhaps it has a number of corner cases or a role involving calculating a highly important
function in your domain. You really want to test for behavior specific to that body of
code, but it’s in a lambda expression and you’ve got no way of referencing it.

As an example problem, let’s look at a method that is slightly more complex than con‐
verting a list of strings to uppercase. Instead, we’ll be converting the first character of a
string to uppercase and leaving the rest as is. If we were to write this using streams and
lambda expressions, we might write something like the following.

Should we want to test this, we’d need to fire in a list and test the output for every single
example we wanted to test. The test below provides an example of how cumbersome this
approach becomes. Don’t worry—there is a solution!

Don’t use a lambda expression! I know that might appear to be
strange advice in an article about lambda expressions, but
square pegs don’t fit into round holes very well. Having
accepted this, we’re bound to ask how we can still unit test
our code and have the benefit of lambda-enabled libraries.

Do use method references. Any method that would have been
written as a lambda expression can also be written as a normal
method and then directly referenced elsewhere in code using
method references. In the code below I’ve refactored out the
lambda expression into its own method. This is then used by the
main method, which deals with converting the list of strings.

The key takeaway here is that if you want to unit test a lambda
expression of serious complexity, extract it to a regular method first. You can
then use method references to treat it like a first-class function.

First Class Functions

Java 8 adds functions as a new form of value. What does this mean? Let’s look at a simple example.

Suppose you want to filter all the hidden files in a directory.
You need to start writing a method that given a File will tell you whether it
is hidden or not. Thankfully there’s such a method inside the File class called
isHidden. It can be viewed as a function that takes a File and returns a
boolean.

However, to use it you need to wrap it into a FileFilter object that you then pass to the File.listFiles method as follows:

Ouch, that’s pretty obscure! We already have a function isHidden that we could use, why do we have to wrap it up in a verbose FileFilter object?

In Java 8 you can rewrite that code as follows:

File[] hiddenFiles = new File(".").listFiles(File::isHidden);

Wow! Isn’t that cool? We already have the function isHidden available so we just “pass” it to the listFiles method. Our code now reads closer to the problem statement.

The use of File::isHidden is a rather special case of a new
feature called method references in Java 8. Given that methods contain code
(the executable body of a method), then using methods as values is like passing
code around.

Sponsor

Project Title

Use this area of the page to describe your project. The icon above is part of a free icon set by Flat Icons. On their website, you can download their free set with 16 icons, or you can purchase the entire set with 146 icons for only $12!

Project Title

Use this area of the page to describe your project. The icon above is part of a free icon set by Flat Icons. On their website, you can download their free set with 16 icons, or you can purchase the entire set with 146 icons for only $12!

Project Title

Use this area of the page to describe your project. The icon above is part of a free icon set by Flat Icons. On their website, you can download their free set with 16 icons, or you can purchase the entire set with 146 icons for only $12!