A little functional Java – the map function

Functional programming is getting a lot of interested nowadays. Languages like Clojure or Scala are on the rise and even languages like Haskell which where formerly considered to be mostly academic are getting more and more popular.
And more and more Java programmers are thinking about functional programming. Java 8 will get lambda expressions which will be a first step towards a more functional programming style. And there are already libraries available that help Java developers to program in a more functional style even if it is not as elegant as using Scala or Clojure.

Today I want to write a little about the map function (not to be confused with the map data structure, e.g. a HashMap in Java) which is popular in all functional programming languages I know.
I is very simple to understand. It is used on a collection (often a list) of values and is given a function as a parameter that is applied to every element in that collection.

A first example in Scala

Let’s look at a few examples in Scala before we have a look at how to do it in Java.

If you print squared you will see:

It shouldn’t be difficult to figure out what’s going on here. The map method is invoked an the numbers List. The argument passed to map is a function (in Scala you can pass functions to other functions or methods) and this function is then applied to all values of the collection the map method is called on and a new collection (here a scala.collection.immutable.List) is returned with the results of the supplied function applied on every element.
In Scala this is so common that you can write it a little shorter:

The _ is a placeholder for the current element on which passed is applied function.

This is just a very simple function but you can passed every function to map in Scala, giving you a very powerful tool to work with collections. In fact the Scala collection library offers A LOT more than just map – be sure to check it out. One example can be found on my blog here.

Just for the curious, here is how to do it in Clojure:

Using Java

Java (before Java 8 at least) doesn’t allow you to pass functions to other methods/functions like Scala. But you can still use something similar using functions as objects.
There are several libraries out there that help Java programmers with functional programming. One I recently came across is totallylazy. It doesn’t have much documentation yet but is under active development and the unit tests and the source code are helpful to figure out how to use it.

Here is a simple example that shows how to use map:

It should again be pretty obvious what’s going on here. We call map on a Sequence object and pass it increment() (which is a com.googlecode.totallylazy.Callable1) which comes with totallylazy and just increments every value in the Sequence.

totallylazy comes with a bunch of useful Callables but often you will want to supply your own version. Here is an example that multiplies every element by a given factor:

Here we created our own Callable1 and pass it to map. It will print:

Again, this is not very difficult and you can check the totallylazy source code to see for many more examples. Of course you can also pass a anonymous class for a Callable1 if you only need it locally and don’t want to reuse it.

There are also methods called mapConcurrently that call map (surprise!) concurrently.

An alternative to totallylazy is Functional Java which has been under development for longer and might be a better choice at the moment for production ready code.

Both libraries offer a lot more than just map.

If you only need map, it is not difficult to write something similar like Callable1 yourself.

As you can see, although not difficult in Java, it requires a lot more code than the examples in Scala or Clojure.
If you want to do a lot of functional programming I highly recommend using a library like totallylazy or Functional Java – or even better a functional language like Scala or Clojure.
Scala gives you the power of both OOP and FP and let’s you choose which is best for a given task.
And even if you don’t want to use Scala or Clojure I highly recommend learning them. It will make you a better Java programmer!

If you want to look at a pure functional language (a very good idea to understand the ideas behind FP), have a look at Haskell. A good starting point is the freely available book: Learn You a Haskell