// Java program to demonstrate Implementation of // functional interface using lambda expressions classTest { publicstaticvoidmain(String args[]) { // lambda expression to create the object new Thread(()-> {System.out.println("New thread created");}).start(); } }
// Java program to demonstrate lamda expressions to implement // a user defined functional interface. @FunctionalInterface interfaceSquare { intcalculate(int x); } classTest { publicstaticvoidmain(String args[]) { int a = 5; // lambda expression to define the calculate method Square s = (int x)->x*x; // parameter passed and return type must be // same as defined in the prototype int ans = s.calculate(a); System.out.println(ans); } }
// A simple program to demonstrate the use // of predicate interface import java.util.*; import java.util.function.Predicate; classTest { publicstaticvoidmain(String args[]) { // create a list of strings List<String> names = Arrays.asList("Geek","GeeksQuiz","g1","QA","Geek2"); // declare the predicate type as string and use // lambda expression to create object Predicate<String> p = (s)->s.startsWith("G"); // Iterate through the list for (String st:names) { // call the test method if (p.test(st)) System.out.println(st); } } }