Do more with less: Lambda expressions in Java 8

Using lambda expressions can make your Java code leaner, more powerful, and easier to read

One of the recurrent, valid criticisms of Java as a programming language is that it is wordy. You have to write a lot of code to get anything done. Lambda expressions relieve that problem for some common cases and give Java useful facilities that have boosted rival C# for years.

Lambda expressions are new to Java SE 8, and in my opinion they are the most significant language features debuting in this version. Lambda expressions provide a relatively clear and more concise way to represent a method interface using an expression. Lambda expressions also improve the Java Collection libraries, making it easier to iterate through, filter, and extract data from Collections. New concurrency features enabled by lambda expressions improve Java runtime performance in multicore environments, which applies to the vast majority of new devices and computers.

[ Nashorn: JavaScript made great in Java 8 | 15 things we hate about Java | Keep up with the latest developer news with InfoWorld's Developer World newsletter. ]

I've heard programmers express the fear that lambda expressions will pollute the object-oriented nature of Java with functional programming constructs. I heard similar noises six or seven years ago in the .Net community. As it happens, the C# language became better for the addition.

Lambda expressions and LINQ in C# and VB.Net
Lambda expressions were added to C# and VB.Net for Visual Studio 2008, primarily to support LINQ (Language-Integrated Query). Lambda expressions are anonymous functions you can use to create delegates or expression tree types. In C#, to create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator =>, and you put the expression or statement block on the other side. For example, the lambda expression x => x * x specifies a parameter that's named x and returns the value of x squared. In VB.Net, you use anonymous Function or Sub definitions to create lambda expressions.

LINQ is a set of features, also introduced in Visual Studio 2008, that adds query capabilities to the language syntax of C# and Visual Basic. LINQ is useful for querying SQL databases, XML documents, ADO.Net data sets (which may or may not have originated from SQL databases), and .Net collections, files, strings, and so on. The last facility is called LINQ to Objects. The term refers to the use of LINQ queries with any IEnumerable or IEnumerable<T> collection directly. Lambdas are used in method-based LINQ queries as arguments to standard query operator methods such as where.

LINQ and lambda expressions have found wide adoption in the C# community. I expect lambda expressions and their application to Collections to catch on in the Java community in a big way.

Anonymous inner classes in Java
Anonymous inner classes in Java are a baby step toward lambda expressions. You can use them by simply defining the class inline without a name, for example:

JButton testButton = new JButton("Test Button");
testButton.addActionListener(new ActionListener()
  {@Override public void actionPerformed(ActionEvent ae){
     System.out.println("Click Detected by Anon Class");  

In this example (from an Oracle tutorial) the ActionListener added to the button is defined in an anonymous inner class with an actionPerformed method instead of using a separate named class. This saves a little code, but it's still wordy.

Interfaces that define only a single method, previously called Single Abstract Method interfaces, are called functional interfaces in Java 8. As we'll see, functional interfaces combined with anonymous inner classes are often used with lambda expressions.

1 2 Page 1
Page 1 of 2