Using Apache Commons Functor functional interfaces with Java 8 lambdas

Apache Commons Functor (hereon [functor]) is an Apache Commons component that provides a functional programming API and several patterns implemented (visitor, generator, aggregator, etc). Java 8 has several nice new features, including lambda expressions and functional interfaces.
In Java 8, lambdas or lambdas expressions are closures that can be evaluated and behave like anonymous methods.
Functional interfaces are interfaces with only one method. These interfaces can be used in lambdas and save you a lot of time from writing anonymous classes or even implementing the interfaces. [functor] provides several functional interfaces (thanks to Matt Benson). It hasn’t been released yet, but there are some new examples in the project site, in the trunk of the SVN.
I will use one of these examples to show how [functor] functional interfaces can be used in conjunction with Java 8 lambdas. After the example with [functor] in Java 8, I will explain how I am running Java 8 in Eclipse (it’s kind of a gambiarra, but works well).

[functor] example

Here is a simple example with one Predicate.


         numbers = Arrays.asList(1, 2, 3, 4);


         isEven = new UnaryPredicate

        () {
    public boolean test(Integer obj) {
        return obj % 2 == 0;

for( Integer number : numbers ) {
    if (isEven.test(number)) {
        System.out.print(number + ' ');

It prints only the the even numbers, those that pass by the predicate test.

[functor] example with lambdas

This modified version is using Java 8 lambdas

List numbers = Arrays.asList(1, 2, 3, 4);
UnaryPredicate isEven = (Integer obj) -> { return obj % 2 == 0; };
for( Integer number : numbers ) {
    if (isEven.test(number)) {
        System.out.print(number + " ");

The behaviour is the same. UnaryPredicate is a functional interface. Its only method is boolean test(A obj);. And when used in a lambda expression you just have to provide the right number of arguments and implement the closure code.

The difference in the two code snippets are the way that the UnaryPredicate for even numbers is created. Below you can see the two ways of creating this predicate, with and without Java 8 lambdas.

// pre-java-8
UnaryPredicate isEven = new UnaryPredicate() {
    public boolean test(Integer obj) {
        return obj % 2 == 0;
// with lambda-8
UnaryPredicate isEven = (Integer obj) -> { return obj % 2 == 0; };

Java 8 in Eclipse

Eclipse 8 doesn’t support Java 8, so you have to create a new builder in order to have Eclipse compiling your project’s sources. For a complete step-by-step guide on how to set up Eclipse Juno and Java 8, please refer to I will summarize the steps here, and will show how to include [functor] jar to the project classpath.

  • Download the JDK from and install it (I installed in /opt/java/jdk1.8.0)
  • Create a new Java project in Eclipse (try-lambdas in my case)
  • Disable the default Java Builder from your Eclipse project, as it doesn’t work with Java 8
  • Create a new builder. When prompted with a screen that lets you browse for a program, select Java 8 javac (for me it was /opt/java/jdk1.8.0/bin/javac)
  • Add the arguments below to your builder:
    -classpath %CLASSPATH%;commons-functor-1.0-SNAPSHOT-jar-with-dependencies.jar;.
    -source 8
    -d ${workspace_loc:/lambdas}/bin

You have to include [functor]‘s jar, as well as its dependencies. For the sake of convenience, I used maven-assembly-plugin to generate a jar with dependencies for [functor]. The code and the jar are available from this GitHub repository. Or if you prefer generate your own [functor] jar with dependencies, check out the code from the repository as below.

svn checkout commons-functor

And finally include the following to [functor] pom.xml before running mvn clean assembly:assembly.



Reference: Using Apache Commons Functor functional interfaces with Java 8 lambdas from our JCG partner Bruno Kinoshita at the Kinoshita’s blog blog.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

One Response to "Using Apache Commons Functor functional interfaces with Java 8 lambdas"

  1. Yuri NotForPublc says:

    now we have Google Guava so Apache Commons Functor may be forgotten

Leave a Reply

× nine = 36

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: