Ivar Conradi Østhus

Menu

Getting functional in Java 8

In September I attended JavaOne 2013 in San Francisco. Oracle was showing off Java 8, scheduled for GA in Q1 2014. The feature comming in Java 8 which exited me the most was the functional part, introduced with Project Lambda.

All the other major platform, such as C#, has had this for years now and finally Java is growing up and will introduce functional programming in Java 8. In previous versions of Java we have been so used to imperative style programming that it is hard to even realize the alternatives. It has worked fine, but is very low level and a extremely verbose syntax compared to the alternatives. With the new functional features we are now able to express what we want to achieve more consciously and not worry so much about how to actually do it. Java 8 enables us to used old school rock solid OO design and combine it with functional patterns. Combined we will be able to achieve more with less, meaning fewer bugs and more value delivered. This is a big change for Java, even bigger than generics introduced a few years back.

In this post I will present a few simple examples on how you can utilize functions in Java 8.

Setup

As a basis for each example I will have a list of persons as showed in the snipped below.

Passing functions in Java 8

Say you want to print the name of each person in the list. How do we do that in Java today (pre Java 8)? EASY! We loop the list and for each item in the list we print the name. We even use the enhanced for loop. Pretty simple, right?

for(Person p : persons) {
System.out.println(p.getName());
}

This is referred to imperative code syle. There is mainly two problems with this example:

We have to introduce a temporary variable (p)

We have to know HOW to iterate a list (the for ioop

Not only do we express WHAT we want to do, we also have to express HOW to do it, iterating all the elements and introducing a mutable element. In Java 8, we now have a forEach method on collections, which allows us to pass a function. The underlying framework will take care of how to loop each element. We will need to pass a Consumer, which performs an operation on each element:

persons.forEach(p -> System.out.println(p));

Remove elements

The collection also makes it super simple to remove elements from a collection. We just use a lambda expression, a predicate, to express which elements we want removed. How would you implement this pre Java 8?

persons.removeIf(p -> p.getAge() > 20);

* We could also use the syntax “(Person p) -> p.getAge() > 20)”, to specify type, this is optional, as it is automatically inferred by the compiler.

WARNING. I generally do not feel it is a good practice to use this function as it mutates the actual list. In my opinion it would be better if it returning a new list/view, without the elements matched by the predicate.

Method references

In Java 8 we will also be able to borrow functions from other classes using the “::” notation: