Google

May 27, 2014

Learning to write functional programming with Java 8 with examples -- part 2

In the last post I created custom functional interfaces to explain functional programming, closure, and lambda expressions in Java 8. This post gives Java 8 API examples using lambda expressions and functional programming.

Example 1: The java.lang.Runnable interface has been made a functional interface.


package com.java8.examples;

public class Example1 {
 
 public static void main(String[] args) {
   System.out.println("main thread: " + Thread.currentThread().getName());
   
   //run method takes no arguments so: () -> (body)
   //closure worker thread1
   Runnable r = () -> (System.out.println("worker thread: " + Thread.currentThread().getName())); 
   new Thread(r).start(); //pass the Runnable closure to Thread constructor
   
   //closure worker thread2
   new Thread( () -> (System.out.println("worker thread: " + Thread.currentThread().getName()))).start(); 
 }
}


Output:

main thread: main
worker thread: Thread-0
worker thread: Thread-1


Example 2: The java.util.function package has a number of functional interfaces.

package com.java8.examples;

import java.util.function.Consumer;

public class Example2 {
 
  public static void main(String[] args) {
   //closure of Consumer 1. the accept(T t) method takes Integer here
   Consumer<Integer>  c1 = (x) -> {System.out.println("consumed: " + x);};
   //closure of Consumer 2. x is an Integer  
   Consumer<Integer>  c2 = (x) -> {System.out.println("consumed: " + x*x);};

   
   c1.accept(5);
   c2.accept(5);
   
   //andThen is a default method implementation in Consumer interface
   Consumer<Integer> c3 = c1.andThen(c2);
   c3.accept(6);
 }
}

Output:

consumed: 5
consumed: 25
consumed: 6
consumed: 36


Example 3: The Function&ltT, R> is similar to Consumer<T>, but the Function interface has a return value in its apply(T t) method, whereas the Consumer  has a void method accept(T t).

package com.java8.examples;

import java.util.function.Function;

public class Example3 {
 
  public static void main(String[] args) {
   //closure of Function.  apply(T t) method takes Double here
   Function<Double, Double>  c1 = (arg) -> (arg + arg);
   Function<Double, Double>  c2 = (arg) -> (arg * arg);
  
   Double result1 = c1.apply(5.0);
   Double result2 = c2.apply(5.0);
   
   System.out.println("result1= " + result1);
   System.out.println("result2= " + result2);
   
   //andThen is a default method implementation in Function interface
   Function<Double, Double> c3 = c1.andThen(c2);
   Double result3 = c3.apply(6.0);
   
   System.out.println("result3= " + result3);// 6 + 6 = 12, 12 * 12 = 144;
   
 }
}


Output:

result1= 10.0
result2= 25.0
result3= 144.0


Example 4: The Predicate<T> interface from java.util.function package has test(T t) method that returns a boolean value indicating test condition has passed or failed. The predicates are often used to  filter a collection of objects.

package com.java8.examples;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class Example4 {
 
 private static final int SHORT_NAME_LENGTH = 3;
 
 public static void main(String[] args) {
  
  List<String> languages = Arrays.asList("Java", "C++", "PHP", "C#", "Scala", "JRuby", "Jython");
  
  //The Predicate interface has test(T t) that returns a boolean
  Predicate<String> shortNamePredicate = (s) -> (s.length() <= SHORT_NAME_LENGTH); // closure
  
 
  Predicate<String> startsWithCPredicate = (s) -> (s.startsWith("J")); //closure
  
  languages.stream()
           .filter(shortNamePredicate)
           .forEach(e -> System.out.println("short names: " + e));
  
  languages.stream()
           .filter(startsWithCPredicate)
           .forEach(e -> System.out.println("starts with J: " + e));
   
 }
}


Output:

short names: C++
short names: PHP
short names: C#
starts with J: Java
starts with J: JRuby
starts with J: Jython


There are more examples relating working with Java collection -> Understanding Java 8 Streams and working with collections using Lambda expressions

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home