@FunctionalInterface

public interface Example {

    void doSomething(Object obj);

}

 

 

 

 

 

package printing;

@FunctionalInterface

public interface Printer {

    void print(Object obj);

}

 

 

 

 

 

package lambda;

import printing.Printer;

public class Adder {

   

    public static void main(String[] args) {

        int num1 = 10;

        int num2 = 20;

        Printer printer =  (Object o) -> {

System.out.printf("The result of %d and %d is %d\n"

, num1, num2, o);

 };

       add(num1, num2, printer);

    }

   

    public static void add(int num1, int num2, Printer printer) {

        int result = num1 + num2;

        printer.print(result);

    }

}

 

 

 

 

 

package lambda;

import java.util.Arrays;

import java.util.List;

import java.util.function.Consumer;

 

public class Main {

    public static void main(String[] args) {

        Consumer c = (Object o) -> { System.out.println(">>>" + o); };

        List l = Arrays.asList("Hello", "World", "Print", "My", "List");

        l.forEach(c);

    }

}

 

 

 

 

package lambda;

 

import java.util.Arrays;

import java.util.LinkedList;

import java.util.List;

 

public class Main2 {

    public static void main(String[] args) {

         List<Integer> l = new LinkedList

 (Arrays.asList(4,5,88,76,55,23,7,20,45));

        l.removeIf(i -> i%2 == 0);

        System.out.println(l);

    }

}

 

 

 

 

package lambda;

public class Detector {

    public boolean isEven(int i) {

        return i%2 == 0;

    }

    public boolean isOdd(int i) {

        return !isEven(i);

    }

}

 

 

 

 

 

package lambda;

import java.util.Arrays;

import java.util.LinkedList;

import java.util.List;

 

public class Main {

    public static void main(String[] args) {

        Detector d = new Detector();

        List<Integer> l = new LinkedList

(Arrays.asList(4,5,88,76,55,23,7,20,45));

        l.removeIf(d::isEven);

        System.out.println(l);

    }

}

 

 

 

 

 

package lambda;

public class Person {

    String firstName,lastName;

    public Person(String firstName, String lastName) {

        this.firstName = firstName;

        this.lastName = lastName;

    }

    public String getFirstName() {

        return firstName;

    }

    public String getLastName() {

        return lastName;

    }

 

    @Override

    public String toString() {

        return  firstName + " " + lastName;

    }

}

 

 

 

 

 

package lambda;

 

import java.util.Arrays;

import java.util.List;

 

public class Main5 {

    public static void main(String[] args) {

        List<Person> l = Arrays.asList(

                new Person("Albert", "Smith"),

                new Person("Jim", "James"),

                new Person("John", "Brown"),

                new Person("William", "Hughes"),

                new Person("Simon", "Fleming")

        );

        l.sort((p1, p2) ->

            p1.getFirstName().compareTo(p2.getFirstName()));

        System.out.println(l);

    }

}

 

 

 

 

 

package lambda;

 

public class Main {

    public static void main(String[] args) {

        NumberAdder na = getNumberAdder();

        System.out.print(na.add(20));

    }

   

    public static NumberAdder getNumberAdder() {

        int startingNumber = 10;

        return x -> x + startingNumber;

    }

}

 

 

 

 

 

package lambda;

 

public class Main7 {

    public static void main(String[] args) {

        NumberAdder na = getNumberAdder();

        System.out.print(na.add(20));

    }

   

    public static NumberAdder getNumberAdder() {

        IntegerHolder ih = new IntegerHolder();

        ih.number = 10;

        NumberAdder na = x -> x + ih.number;

        ih.number = 20;

        return na;

    }

}