Java "Lambdas"

Functional Arguments

  • Frequently it is useful to pass functions as arguments
  • Pass a comparison function to a sort function
  • Assign an event handler to a GUI object (e.g. button)

Functional Arguments in Java

  • Java does not have functions (only methods)
  • Cannot pass a method as an argument
  • Instead, pass an object that has the desired function as a method
  • How to declare the argument?
  • Define an interface that has the method signature

Sorting an ArrayList

// Sort1.java
//   Read integers from standard input, sort them, and write
//   them to standard output.

import java.util.*;

public class Sort1 {
  public static void main(String[] argv) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    Scanner sc = new Scanner(System.in);
    for (int num = sc.nextInt(); sc.hasNextInt(); num = sc.nextInt()) {
      numbers.add(num);
    }
    Collections.sort(numbers);
    for (int num : numbers) {
      System.out.println(num);
    }
  }
}

Sorting Using a Comparator

// Sort2.java
//   Read integers from standard input, sort them in decreasing order,
//   and write them to standard output.

import java.util.*;

public class Sort2 {
  static class Comp implements Comparator<Integer> {
    public int compare(Integer x, Integer y) {
      return y.intValue() - x.intValue();
    }
  }
  public static void main(String[] argv) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    Scanner sc = new Scanner(System.in);
    for (int num = sc.nextInt(); sc.hasNextInt(); num = sc.nextInt()) {
      numbers.add(num);
    }
    Collections.sort(numbers, new Comp());
    for (int num : numbers) {
      System.out.println(num);
    }
  }
}

The Comparator as an Anonymous Class

// Sort3.java
//   Read integers from standard input, sort them in decreasing order,
//   and write them to standard output. Defines a Comparator as an
//   anonymous class.

import java.util.*;

public class Sort3 {
  public static void main(String[] argv) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    Scanner sc = new Scanner(System.in);
    for (int num = sc.nextInt(); sc.hasNextInt(); num = sc.nextInt()) {
      numbers.add(num);
    }
    Collections.sort(numbers, new Comparator<Integer>() {
    public int compare(Integer x, Integer y) {
      return y.intValue() - x.intValue();
    }
      });
    for (int num : numbers) {
      System.out.println(num);
    }
  }
}

The Comparator as a Lambda

// Sort4.java
//   Read integers from standard input, sort them in decreasing order,
//   and write them to standard output. Defines a Comparator as a lambda.

import java.util.*;

public class Sort4 {
  public static void main(String[] argv) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    Scanner sc = new Scanner(System.in);
    for (int num = sc.nextInt(); sc.hasNextInt(); num = sc.nextInt()) {
      numbers.add(num);
    }
    Collections.sort(numbers, (x, y) -> y.intValue() - x.intValue());
    for (int num : numbers) {
      System.out.println(num);
    }
  }
}

When Can We Use a Lambda?

  • Lambdas can be used for functional interfaces
  • A functional interface is one that defines a single method