Tuesday, August 19, 2014

Using Lambdas Java 8 [Java 8][Lambda]

This program shows the usage of Lambdas in Java 8. Make sure that you're using JDK 1.8.0_11 or higher. If you're using Netbeans, configure it to use JDK 8 if not already done.

Lambdas are useful if you are going to learn the JavaFX APIs (they use them a lot). These also make writing much cleaner code possible.

Here's an example. Comments are included wherever necessary.

import java.util.*;
import java.util.function.*;

class Student{
    enum Sex { MALE, FEMALE }
    Student(String name, int id, Sex gender){
        this.name = name;
        this.id = id;
        this.gender = gender;
    String name;
    int id;
    Sex gender;
    public void printStudent(){
        System.out.println(name+" "+id+" "+gender); 

public class LambdaTest {
    static List<Student> students = new ArrayList<>();
    public static <A , B> void filter(Iterable<A> source, Predicate<A> lamPred, Function<A, B> mapper, Consumer<B> output){
        for(A item:source){
                B data = mapper.apply(item);
    public static void main(String[] args) {
        students.add(new Student("SchoolStud", 12, Student.Sex.MALE)); 
        filter(students, student->student.id!=0, p->{return p.name;}, stringg->System.out.println(stringg));
        filter(students, student->student.id==12, p->p.name, stringg->System.out.println(stringg));
        filter(students, s->s.id==12, s->s.name, s->System.out.println(s));

interface MyLambdaType<A, B, C, D>{
    D perform(A a, B b, C c);
//    A anotherFunction(A a, B b); //Uncomment this and you get an error.
//    This interface must only have one abstract method for using it as a Lambda

class AnotherTest{
    public static void main(){
        Predicate<Integer> pred = p->p==11; //Tests if a value is 11
        Function<Integer, String> withReturn = p->p.toString();//Returns the 
        //string representation of Integer
        Consumer<String> printer = p->System.out.println(p);
        //In case of return values
        Function<Integer, String> withReturn1 = p->{return "Use braces if "
                + "explicitly writing return. Don't forget semicolons "
                + "inside braces";};
        BiFunction<Integer, Integer, String> adder = (a, b)->((Integer)(a+b)).toString();
        //If you wanna make your own lamba function using a custom interface, 
        //that interface must have only one abstract method
        MyLambdaType<Integer, Integer, Integer, String> mlt = (a, b, c)->((Integer)(a+b+c)).toString();
        //Now use the lambdas just defined:
        System.out.println(withReturn.apply(1772)); //Get me a string
        //representation of this Integer
        printer.accept("This lambda prints this line");//Print a line for me
        System.out.println(adder.apply(1772, 1772));
        //Now test your own lambda type
        System.out.println(mlt.perform(1, 1, 1));//Perform the addition
        //of these three numbers
        Lambdas are just passable functions


This lambda prints this line
Use braces if explicitly writing return. Don't forget semicolons inside braces

Documentation http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

No comments:

Post a Comment