60

Is there a good library for functional programming in Java?

I'm looking for stuff like Predicate and List.Find() (as a static method). Not complicated to implement, but it would be nice to find a reusable library here.

ripper234
  • 222,824
  • 274
  • 634
  • 905

12 Answers12

44

FunctionalJava is the best known library; it makes use of Java closures (BGGA) for examples:

final Array<Integer> a = array(1, 2, 3);  
final Array<Integer> b = a.map({int i => i + 42});  
arrayShow(intShow).println(b); // {43,44,45}  

EDIT

Check also lambdaj.

Further EDIT

BGGA is entirely optional. It just makes for nicer syntax.

Apocalisp
  • 34,834
  • 8
  • 106
  • 155
dfa
  • 114,442
  • 31
  • 189
  • 228
  • 5
    But BGGA is not actually in Java. So this won't compile, right? – Gabe Moothart Aug 12 '09 at 17:24
  • 9
    BGGA is available as a precompiler that translates {int i => i + 42} into new F() { public Integer f(Integer i) {return i + 42; }}. The latter will compile with any old Java. – Apocalisp Aug 12 '09 at 17:44
  • right, it is a little frustrating indeed – dfa Aug 12 '09 at 17:44
  • Also, the source code is readable and embeddable. I was able to take out some of the classes without the full blown library and make use of functional java. – Berlin Brown Mar 29 '11 at 18:47
30

Scala is a functional programming language that is fully compatible with Java (runs through the JVM). It offers a beautiful mix of object-oriented and functional techniques along with many improvements over Java in generics and concurrency. Some even say it could replace Java.

Lii
  • 11,553
  • 8
  • 64
  • 88
Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
27

Java Libraries

There are libraries that can help you do this, by already doing the legwork for you and hiding the arcane things:

Mature / Established Libraries

More Obscure / Experimental Libraries

These will allow you to write Java code with a more functional approach and possibly more familiar syntax and semantic, as you'd expect from an FP-competent language. Within reason, that is.

JVM Languages

And obviously, you can implement a functional language on top of Java. So that you can then use that one as your FP language. Which is a bit of a higher-level of abstraction than what you asked for, but relatively within context (though I'm cheating a bit here, granted).

For instance, check out:

Quite Mature Languages

Less Mature or More Obscure Languages

Further Reading

You may also want to read or watch these articles or videos:


Taken from my P.SE answer to "Is Functional Programming Possible in Java?"

Community
  • 1
  • 1
haylem
  • 22,460
  • 3
  • 67
  • 96
13

Google collections has a decent selection of functional-programming style utility methods. Some classes of interest are Iterables, Iterators, Function, Functions, etc

It also has a bunch of collection classes as well!

Chi
  • 22,624
  • 6
  • 36
  • 37
12

Functional Java is one that's worth taking a look at and FunctionalJ is another.

mikej
  • 65,295
  • 17
  • 152
  • 131
  • So .. which one? Also, for the lazy among us, can they be used without Java closures? (Assuming I only want a library, not something that forces me to use a different compiler) – ripper234 Aug 12 '09 at 17:30
  • 2
    Functional Java is just a library, but it works with the BGGA closure syntax if you want to use that. – Apocalisp Aug 12 '09 at 17:41
7

If you want a pure Java solution check out lambdaj

http://code.google.com/p/lambdaj/

Besides the possibility to define and use closure in a DSL-style, it also allows to manipulate collections in a functional way, without explicitly write closures or loops

Mario Fusco
  • 13,548
  • 3
  • 28
  • 37
4

Or download OpenJDK 8 to try out Lambda expressions the way they will become in Java 8. Among others, the collection APIs are adjusted to support a functional style. See http://macgyverdev.blogspot.se/2012/10/functional-programming-in-java.html for examples of new collection APIs and comparisons with Guava, LambdaJ and FunctionalJava.

Johan Norén
  • 627
  • 2
  • 7
  • 14
4

Jambda is another FP-library. From the documentation:

Jambda is an attempt to provide the Java(TM) world with tools and concepts from functional programming (FP).

The goals are several:

  • To provide Java programmers with expressive FP constructs
  • To provide a bridge for Java programmers into the FP-world
  • To see how far Java and generics can be stretched

This document is an attempt to introduce Java programmers into the FP world, and at the same time explain some (or most) of the features in Jambda.

Johan Kullbom
  • 4,183
  • 26
  • 28
4

Scala was mentioned here, but there's a lot lighter and more Java compatible language: Xtend. It compiles to plain Java and uses the same type system. It has great Eclipse support. You can mix .java and .xtend files in a single project.

Sample code:

def static void main(String[] args) {
    val s = #[1,2,3].map[it+43].join(", ")
    println(s);
}
rzymek
  • 9,064
  • 2
  • 45
  • 59
4

Apache Commons has some functional-ish code in it. See for example, the Predicate interface.

Tyler
  • 21,762
  • 11
  • 61
  • 90
4

Google Guava has functional:

  • collection operations
  • concurrency constructs (Futures)
thSoft
  • 21,755
  • 5
  • 88
  • 103
2

Although Functional Java is the most popular but i'll suggest you to try Google guava lib.

http://code.google.com/p/guava-libraries/

Amit Mutreja
  • 168
  • 1
  • 7