package localclasses;

public interface Filter<T> {

      boolean filter(T t);

}

 

 

package localclasses;

 

import java.util.ArrayList;

import java.util.List;

 

public class Collections2 {

      public static <T> List<T> filter(

   List<T> input, Filter<T> filter) {

            List<T> result = new ArrayList<T>();

            for (T t : input) {

                  if (filter.filter(t)) {

                        result.add(t);

                  }

            }

            return result;

      }

}

 

 

package localclasses;

 

import java.util.List;

import java.util.Arrays;

 

public class Main {

 

      public static void main(String[] args) {

            List<Integer> numbers = Arrays.asList(1,5,4,2,7,9,6,4,2,6,5,8);

            System.out.println(

Collections2.filter(numbers, new Filter<Integer>() {

                        @Override

                        public boolean filter(Integer t) {

                              return t%2 == 1;

                        }                

                  })

);

      }

 

}

 

 

[1,5,4,2,7,9,6,4,2,6,5,8].filter(function(v) {return v%2 == 1;})

 

 

@FunctionalInterface

public interface Example {

    void doSomething(Object obj);

}

 

 

package printing;

@FunctionalInterface

public interface Printer {

    void print(Object obj);

}

 

 

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

printer.print("Hello World");

 

 

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.List;

 

public class Main {

    public static void main(String[] args) {

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

        l.forEach((Object o) -> { System.out.println(">>>" + o); });

    }

}

 

 

 

package lambda;

 

import java.util.Arrays;

import java.util.List;

 

public class Main {

    public static void main(String[] args) {

        List<String> l = Arrays.asList("Hello", "World",

 "Print", "My", "List");

        l.forEach(str ->  System.out.println(">>>" + str));

    }

}

 

 

 

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;

 

import java.util.Arrays;

import java.util.List;

 

public class Main3 {

    public static void main(String[] args) {

        List<String> l = Arrays.asList("Hello", "World",

"Print", "My", "List");

        l.forEach(System.out::println);

    }

}

 

 

 

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 Main4 {

    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;

 

@FunctionalInterface

public interface NumberAdder {

    int add(int x);

}

 

 

public static NumberAdder getNumberAdder() {

    int startingNumber = 10;

    return x -> x + startingNumber;

}

 

 

package lambda;

 

public class Main6 {

    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;

    }

}