Home > Articles > Programming > Java

📄 Contents

  1. Interface Default and Static Methods
  2. Lambda Expressions and Functional Interfaces
Like this article? We recommend

Lambda Expressions and Functional Interfaces

A lambda expression (lambda) is a short-form replacement for an anonymous class. Lambdas simplify the use of interfaces that declare single abstract methods. Such interfaces are known as functional interfaces.

Lambdas let you treat code as data. You can use lambdas to pass code to methods for subsequent execution. Listing 4 contrasts the traditional (cumbersome) approach to passing code via an anonymous class with the cleaner approach to passing code via a lambda.

Listing 4 LambdaDemo.java (Version 1).

import java.awt.EventQueue;

public class LambdaDemo
{
   public static void main(String[] args)
   {
      Runnable r = new Runnable()
                   {
                      @Override
                      public void run()
                      {
                         System.out.println("Running");
                      }
                   };
      EventQueue.invokeLater(r);

      EventQueue.invokeLater(() -> System.out.println("Running"));
   }
}

Listing 4's main() method first creates a Runnable instance the old-fashioned way, by instantiating an anonymous class that implements the Runnable interface. This instance is then passed to java.awt.EventQueue's static void invokeLater(Runnable r) method.

The traditional approach to creating and passing a block of code to execute is cumbersome and results in a loss of clarity. In contrast, all of this code can be replaced by EventQueue.invokeLater(() -> System.out.println("Running"));, which is much more compact.

The () -> System.out.println("Running") expression is an example of a lambda. It describes a functional interface whose single abstract method doesn't have a parameter list (hence the ()), and it executes System.out.println("Running")); when invoked.

A lambda doesn't have an explicit interface type. Instead, the compiler infers from the context which functional interface to instantiate. In Listing 4, it knows that invokeLater() requires a Runnable parameter, and it instantiates an implementation of this functional interface.

Compile Listing 4 as follows:

javac LambdaDemo.java

Run the LambdaDemo application as follows:

java LambdaDemo

You should observe the following output:

Running
Running

    If you're not convinced that lambdas are an important contribution to the Java language, I recommend that you read Mario Fusco's blog post "Why We Need Lambda Expressions in Java - Part 1." Fusco makes the case for using lambdas in the context of the internal iteration of a collection, optimizing this iteration by processing items in parallel. After all, lambdas were designed to support a multicore processor architecture, which relies on software that provides parallelism, which in turn improves performance and reduces an overall task's completion time.

Lambda Syntax and Target Types

A lambda adheres to the following syntax:

( formal-parameter-list )  -> { expression-or-statements }

formal-parameter-list is a comma-separated list of formal parameters that match the formal parameters of the functional interface's single abstract method. If you don't specify their types, the compiler infers the parameter types from the context. Consider the following examples:

(int x, int y)
(x, y)

If formal-parameter-list consists of a single parameter, the parentheses can be omitted. However, if formal-parameter-list is empty (there are no parameters), the empty parentheses must be specified. Here are a couple of examples:

x
()

Following the -> operator is expression-or-statements, which is an expression or a block of statements (the lambda body). If you place the expression between the opening and closing braces ({ }), you must use a return statement to return its value; otherwise, don't use return. Consider these examples:

(int x, int y) -> x+y
(x, y) -> { return x+y; }
(int x, int y) -> { System.out.println(x+y); return x+y; }

Listing 5 puts the previous theory to practical use by showing how you might use lambdas in an application that converts between different kinds of units.

Listing 5 LambdaDemo.java (Version 2).

@FunctionalInterface
interface Converter
{
   double convert(double input);
}

public class LambdaDemo
{
   public static void main(String[] args)
   {
      // Convert Fahrenheit to Celsius
      System.out.println(convert(input -> (input-32)*5.0/9.0, 98.6));

      // Convert Kilometers to Miles
      System.out.println(convert(input -> input/1.609344, 8));
   }

   static double convert(Converter converter, double input)
   {
      return converter.convert(input);
   }
}

Listing 5 introduces a functional interface named Converter that declares a double convert(double input) method for converting some input value to an output value. It also introduces a LambdaDemo class whose main() method demonstrates this functional interface.

The Converter functional interface is demonstrated in the context of a static double convert(Converter converter, double input) method. The lambda makes it easy to pass code as data to this method, which is ultimately received as a Converter instance.

Compile Listing 5 (javac LambdaDemo.java) and run the application (java LambdaDemo). You should observe the following output:

37.0
4.970969537898672

Associated with a lambda is an implicit target type (the type of object to which a lambda is bound). Because the target type must be a functional interface and is inferred from the context, lambdas can appear in only the following contexts:

  • Variable declaration
  • Assignment
  • Return statement
  • Array initializer
  • Method or constructor arguments
  • Lambda body
  • Ternary conditional expression
  • Cast expression

I've created another version of LambdaDemo that demonstrates these contexts. Check out Listing 6.

Listing 6 LambdaDemo.java (Version 3).

import java.awt.EventQueue;

import java.security.AccessController;
import java.security.PrivilegedAction;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import java.util.concurrent.Callable;

public class LambdaDemo
{
   public static void main(String[] args) throws Exception
   {
      // Target type: variable declaration
      Runnable r = () -> { for (int i = 0; i < 5; i++)
                              System.out.println(i); };
      r.run();
      
      // Target type: assignment
      r = () -> System.out.println("running");
      r.run();

      // Target type: return statement (in getRunnable())
      EventQueue.invokeLater(getRunnable("I'm running"));

      // Target type: array initializer
      Callable<String>[] callables = new Callable[]
      { 
         () -> "First callable", 
         () -> "Second callable",
         () -> "Third callable"
      };
      System.out.println(callables[1].call());

      // Target type: method or constructor arguments
      EventQueue.invokeLater(() -> System.out.println("invoked later"));

      // Target type: lambda body (a nested lambda)
      Callable<Runnable> callable = () -> () -> 
         System.out.println("callable says hello");
      callable.call().run();

      // Target type: ternary conditional expression
      boolean ascendingSort = false;
      Comparator<String> cmp;
      cmp = (ascendingSort) ? (s1, s2) -> s1.compareTo(s2) 
                            : (s1, s2) -> s2.compareTo(s1);
      List<String> planets = Arrays.asList("Mercury", "Venus", "Earth", "Mars",
                                           "Jupiter", "Saturn", "Uranus", 
                                           "Neptune");
      Collections.sort(planets, cmp);
      for (String planet: planets)
         System.out.println(planet);

      // Target type: cast expression
      String user = AccessController.doPrivileged((PrivilegedAction<String>) () 
                                           -> System.getProperty("user.name"));
      System.out.println(user);
   }

   static Runnable getRunnable(String s)
   {
      return () -> System.out.println(s);
   }
}

The final example demonstrates the occasional need for a cast, which happens to be (PrivilegedAction<String>). The cast is present to handle ambiguity, which arises from the java.security.AccessController class declaring the following methods:

static <T> T doPrivileged(PrivilegedAction<T> action)
static <T> T doPrivileged(PrivilegedExceptionAction<T> action)

Each of the java.security.PrivilegedAction and java.security.PrivilegedExceptionAction functional interfaces declares the same T run() method, and the compiler isn't sure which interface is the target type. Without the cast, the compiler would report an error.

Compile Listing 6 (javac LambdaDemo.java) and run the application (java LambdaDemo). You should observe the following output (notice that the sorting of planet names is in descending alphabetical order because I assigned false to ascendingSort):

0
1
2
3
4
running
Second callable
I'm running
invoked later
callable says hello
Venus
Uranus
Saturn
Neptune
Mercury
Mars
Jupiter
Earth
Owner

Scopes, Local Variables, this, super, and Exceptions

A lambda doesn't define a new scope. Instead, its scope is the same as the enclosing scope. For example, if a lambda body declares a local variable with the same name as a variable in the enclosing scope, the compiler will report an error.

Whether declared in the lambda body or in the enclosing scope, a local variable must be initialized before being used. Otherwise, the compiler reports an error.

A local variable or parameter that is declared outside of a lambda body and referenced from the body is required to be declared final or to be effectively final (so that it hangs around) because the lambda may execute long after the outer code has returned and non-final/non-effectively final variables no longer exist. An effectively final variable isn't modified after being initialized. If a variable is modified, the compiler reports an error.

Listing 7 demonstrates these local variable-related problems.

Listing 7 LambdaDemo.java (Version 4).

@FunctionalInterface
interface Justifier
{
   String justify(String text, int width);
}

public class LambdaDemo
{
   public static void main(String[] args)
   {
      String title = "Hamlet";

      //StringBuilder sb;
      Justifier justifier = (text, width) ->
                            {
                               StringBuilder sb = new StringBuilder();
                               int count = 0;
                               //title = "";
                               for (int i = 0; i < text.length(); i++)
                               {
                                  char ch = text.charAt(i);
                                  if (++count == width)
                                  {
                                     sb.append("\r\n");
                                     count = 0;
                                  }
                                  sb.append(ch);
                               }
                               return sb.toString();
                            };

      String text = "This above all: to thine own self be true,"+
                    "And it must follow, as the night the day,"+
                    "Thou canst not then be false to any man.";
      System.out.println(justify(justifier, text, 10));
   }

   static String justify(Justifier justifier, String text, int width)
   {
      return justifier.justify(text, width);
   }
}

To observe the problem where a lambda body declares a local variable with the same name as a variable in the enclosing scope, uncomment StringBuilder sb; and recompile. You should observe an error message about sb already being defined in main().

To observe the problem where a local variable isn't initialized before being used, uncomment StringBuilder sb; comment out StringBuilder sb = new StringBuilder();, and recompile. You should observe an error message about sb not being initialized.

Finally, to observe the problem of attempting to modify an effectively final variable, uncomment title = ""; and recompile. You should observe an error message telling you that local variables referenced from a lambda must be final or effectively final.

Compile Listing 7 (javac LambdaDemo.java) and (assuming successful compilation) run the LambdaDemo application (java LambdaDemo). You should observe the following output (on a Windows platform):

This abov
e all: to 
thine own 
self be tr
ue,And it 
must follo
w, as the 
night the 
day,Thou c
anst not t
hen be fal
se to any 
man.

There are two more items to consider. First, any this or super reference that appears in a lambda body is the same as in the enclosing scope because a lambda doesn't introduce a new scope, which is not the case with anonymous classes. Consider Listing 8.

Listing 8: LambdaDemo.java (Version 5)

public class LambdaDemo
{
   public static void main(String[] args)
   {
      new LambdaDemo().doSomething();
   }
 
   public void doSomething()
   {
      System.out.println(this);
      Runnable r = new Runnable()
                       {
                          @Override
                          public void run()
                          {
                             System.out.println(this);
                          }
                       };
      new Thread(r).start();
      new Thread(() -> System.out.println(this)).start();
   }
}

Listing 8's main() method instantiates LambdaDemo and invokes the instance's doSomething() method. This method outputs the object's this reference, instantiates an anonymous class that implements Runnable, creates a java.lang.Thread object that executes this Runnable when its thread is started, and creates a second Thread object whose thread executes a lambda when started.

Compile Listing 8 (javac LambdaDemo.java) and run the LambdaDemo application (java LambdaDemo). You should observe output that's similar to the following output:

LambdaDemo@15db9742
LambdaDemo$1@293ed848
LambdaDemo@15db9742

The first output line shows the LambdaObject this reference, the second output line shows a different this reference in the new Runnable scope, and the third output line shows the this reference in a lambda context. The third output line matches the first output line because the lambda's scope is nested inside the doSomething() method; this has the same meaning throughout this method.

Second, a lambda body must not throw more exceptions than are specified in the throws clause of the functional interface method. If a lambda body throws an exception, the throws clause of the functional interface method must declare the same exception type or its supertype.

Conclusion

Interface default/static methods and lambda expressions/functional interfaces are not the only new language features offered by Java 8. In Part 2 of this series (coming April 22, 2014), I cover predefined functional interfaces, method references, enhanced generic type inference, and type annotations.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020