1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
/****author: Bindiya Patil****/ /****forEach()method****/ package program; import java.util.ArrayList; import java.util.List; public class ForEachmethod { public static void main(String[] args) { List<String> fruits = new ArrayList<String>(); fruits.add("Apple"); fruits.add("Orange"); fruits.add("Banana"); fruits.add("Pear"); fruits.add("Mango"); //lambda expression in forEach Method fruits.forEach(str->System.out.println(str)); } } /****functional interface***/ package program; public interface functional_interface { public int add(int a, int b); } /****Lambda Expression****/ package program; public class lambda_expression { public static void main(String[] args) { functional_interface d2=(a,b)-> (a+b); System.out.println(d2.add(10, 20)); } } /***Stream API*****/ package program; import java.util.ArrayList; import java.util.List; public class Stream_API { public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ajeet"); names.add("Negan"); names.add("Aditya"); names.add("Steve"); //Using Stream and Lambda expression long count = names.stream().filter(str->str.length()<6).count(); System.out.println("There are "+count+" strings with length less than 6"); } } |
Hi Bindiya, did u also created a PPT for the same. Code is ok but you need to add some explanations as well? For example what is the use of functional interface, why we need lambda expression and why we use Stream API
ok sir … from next time onwords I will try all the things that you are added in feedback.