Java 8

Java 8 is a radical extension of Java 7. It includes lambdas, streams, and other features. There are lots of online tutorials.

Lambdas

A lambda is a function object. It can be called like a function, or processed like an object. (Processed means assigned to variables, passed to functions, and returned by functions).

Function Interfaces

A complete list of Function Interfaces and their methods can be found here:

java.util.function summary

To use these interfaces you must import them using:

import java.util.function.*;

Demo1: Creating and Calling Lambdas

Lambda syntax is (params)->body. Here are a few examples:

public class LambdaDemo1 {
  
   public static void  main(String args[]) {
     
      Function<Integer, Boolean> f; // f: Integer -> Boolean
     
      f = (Integer x) -> { return x % 2 == 0; };
      System.out.println(f.apply(100));
      System.out.println(f.apply(101));
      System.out.println(f.apply(102));
     
      f = (x) -> 0 < x;
      System.out.println(f.apply(100));
      System.out.println(f.apply(-100));
     
      f = (x) -> {
         Boolean result = true;
         int i = 2;
         while(result && i < x) {result = x % i != 0; i++;}
         return result;
      };
      System.out.println(f.apply(17));
      System.out.println(f.apply(18));
      System.out.println(f.apply(19));
   }
}

Output

true
false
true
true
false
true
false
true
  

Demo2: Returning Lambdas

Lambda's use the static scope rule: search the defining environment for non-locals, not the calling environment.

public class LambdasDemo2 {
  
     
   public static Function<Integer, Integer> makeAdder(Integer n) {
      return (Integer m) -> n + m;
   }
  
   public static void main(String args[]) {
      Integer n = 50; // not used by lambda
      Function<Integer, Integer> f = makeAdder(5);
      System.out.println("f(9) = " + f.apply(9)); // n = 5
   }
}

Output

f(9) = 14

Demo3: Referencing methods

Assume an Account class is defined:

class Account {
   private static Integer nextID = 0;
   private Double balance;
   private Integer id;
   public static Integer getNextID() { return nextID++; }
  
   public Account(Double balance) {
      this.balance = balance;
      id = getNextID();
   }
   public Double getBalance() { return balance; }
   public void deposit(Double amt) { balance += amt; }
   public void withdraw(Double amt) { balance -= amt; }
}

We can reference the Account methods as follows:

public class LambdasDemo3 {
  
     
   public static void main(String args[]) {
      Account savings = new Account(20.0);
      Consumer<Double> f = savings::deposit; // f: Double -> void
      f.accept(10.0);
      f = savings::withdraw;
      f.accept(15.0);
      Supplier<Double> g = savings::getBalance; // g: void -> Double
      System.out.println("balance = $" + g.get());
     
     
     
      Supplier<Integer> h = Account::getNextID;
      System.out.println("next ID = " + h.get());
     
      BiConsumer<Account, Double> m = Account::withdraw;
      m.accept(savings, 8.0);
      System.out.println("balance = $" + g.get());
     
   }
}

Output

balance = $15.0

next ID = 1

balance = $7.0