Over a million developers have joined DZone.

Functional Programming Concepts in JDK 7

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

There's much excitement about JDK 7 and in particular Lambdas! I've waded through the bloat to help you get an understanding of it.

If you search for JDK 7 in your favourite search engine the chances are you'll hit the controversies surrounding lambadas in Java fairly early on in your hunt. It's a contentious subject, which means it's getting a lot of attention from a lot of clever people, but this in turn makes the process slow and adds difficulty in making decisions.

My take is that lambdas will be in JDK 7 - you can see plenty of evidence of that around the web and in the snapshot builds. That said, no decision is concrete (which is a wise tip from The Pragmatic Programmer no less!). This article is aimed at those who don't know much about functional programming or what Lambdas, Closures or Currying are and want to get 'primed'.


Lambdas are not a new concept. They've been around since the '30s in the wider topic of Lambda Calculus, introduced by Alonzo Church. Since then they've been a major feature in many functional programming languages - the most prominent of which would probably be Lisp. The subject itself is therefore quite extensive, but in its simplest form it is fair to state that Lambda is the feature of having functions as first-class citizens in a language; that's to say they are treated as an object in their own right by the type system. A Lambda is an anonymous function. To demonstrate, here's a snippet of Python that you might commonly see:

list = [1, 2, 3, 6, 8,]
print filter(lambda x: x * 2 > 10, list)
[6, 8]

The expression "lambda x: x * 2 > 10" is a lambda function - one that is anonymous at runtime and executed in the filter function. Similarly, you could assign the same expression to a variable and pass that in, or alternatively call the function itself:

f = lambda x: x * 2 > 10
print filter(f, list)
[6, 8]
# Let's call f itself

In Java we currently have Anonymous Inner Classes which slightly demonstrate Lambdas in that they are anonymous and objects in their own right (because they're just compiled into an inner class). Once passed to a method they're just an everyday object. An example would be:

File cwd = new File(".);
System.out.println(cwd.list(new FileFilter() {
public boolean accept(File f) {
return f != null && f.getName().endsWith(".java");

For now, think of lambdas simply as an anonymous functions which are as objects in their own right. In Java this could mean 'callbacks', true recursion and other wonderful things - perhaps gone are the days of implementing the Comparator interface to filter a Collection. Should they be completed in JDK 7, I personally look forward to seeing a much more dynamic language that'll have a whole new lease of life!

Function Types

As alluded to previously, lambdas would intrinsically introduce another concept of functionaly programming: function types. This simply refers to having fucntions as objects, just like a String or BigDecimal. This allows you to pass them to other functions (or indeed return them - see "higher order functions" below) like you would any other type.


You may also have heard of another term often thrown about in the debate - 'Closures'. This is another aspect of functional programming that is quite simple. A closure is the ability to have a function within another function and for that inner function to refer to variables outside of its scope; such variables are said to be "free" because they are not parameters of the closure or local variables. Think of the final variables that are available to anonymous inner classes in Java from their enclosing method.

In fact, if you have read much on this subject with regards to JDK 7 you'll probably see the term closure more than lambdas, this is because the two are often seen together - lambdas become closures when they access variables outside their scope and closures are lambdas by definition. 

Higher order functions

A higher order function is one that takes or returns a another function. In the Python examples at the beginning of this article you would've seen use of the 'filter' function. This itself is a higher order function because its first parameter is a function which is used to test the contents of each element of the list (the second parameter). 


Currying can be best explained as 'chaining' lambdas. Given a function that takes multiple arguments, curryingis the process of transforming that function so that each argument is passed to a function returned by each original function call. To demonstrate:

// "mul" is function that takes two arguments; both ints
mul(5).(5); // returns 25

Some "nice to haves"

Other functional programming concepts that I think might be nice to have are:

  • List comprehensions - syntactic sugar for generating lists (or sets, maps etc) as "one-liners"; e.g. List<String> l = (for x in someOtherCollection) { x.someMethod() && x.another(); }
  • Tail recursion - the ability for the compiler (or runtime environment) to identify a function as calling itself in the last of its instructions, therefore allowing for significantly optimised code.  

What's the point?

Good question. There's plenty of reasoning, the most obvious to me would be less code being written overall and another avenue of abstraction. Instead of implementing interfaces to pass around for FilenameFilters, Comparators and the like, you can pass functions which syntactically require less bloat.

A good source of 'use case' based examples is Neil Gafter's blog (who tends to know a few things about Java :)), start here and here.


The world of programming is rife with alternative paradigms, new age thinking and tradition. Functional programming is just another excellent approach to software development that provides its own set pros and cons; as with all programming features it's another tool that may or may not aid you in your quest to writing better software. Regardless of its use, I believe Project Lambda - should it be completed - could provide a breath of fresh air to a language that's still very popular but has its faults. 



Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}