1. 基于Function函数式使用Lambda表达式
Function<T,R>:接收一个参数并返回结果的函数;R apply(T t):将此参数应用到函数中
1import java.util.Arrays; 2import java.util.Collections; 3import java.util.List; 4import java.util.function.Function; 5/** 6 * 获取集合最大值 7 */ 8public class LambdaFace { 9 private static List<Integer> list = Arrays.asList(1, 2, 13, 14, 18, 101); 10 //接收一个参数并返回结果的函数,即接收一个List集合,返回一个Integer数据 11 public static void getMax(Function<List<Integer>, Integer> function){ 12 //将此list参数应用到Function函数式接口中,并输出其返回的结果 13 System.out.println(function.apply(list)); 14 } 15 public static void main(String[] args) { 16 //调用方法,使用lambda表达式,n为传入的参数,利用Collections.max()方法获取集合最大值,并返回 17 getMax(n -> { 18 Integer max = Collections.max(n); 19 return max; 20 }); 21 } 22} 23
2. 基于BiFunction函数式使用Lambda表达式
BiFunction<T,U,R>:接受两个参数并返回结果的函数;R apply(T t, U u):将参数应用于函数执行
1import java.util.ArrayList; 2import java.util.Arrays; 3import java.util.List; 4import java.util.function.BiFunction; 5 6/** 7 * 获取两个List集合中的重复元素 8 */ 9public class Lambda_BiFunction { 10 private static List<Integer> list1 = Arrays.asList(1,3,5,7,9); 11 private static List<Integer> list2 = Arrays.asList(1,2,4,6,9); 12 public static void getSame(BiFunction<List<Integer>,List<Integer>,List<Integer>>biFunction){ 13 for(Integer num : biFunction.apply(list1,list2)){ 14 System.out.println(num); 15 } 16 } 17 public static void main(String[] args) { 18 getSame((n,m) -> { 19 //存放重复数据 20 List<Integer> result = new ArrayList<Integer>(); 21 for (Integer num : n) {//遍历n 22 if (m.contains(num)) {//如果存在这个数 23 result.add(num);//放进一个list里面,这个list就是交集 24 } 25 } 26 return result; 27 }); 28 } 29} 30
3. 基于Predicate函数式使用Lambda表达式
Predicate<T>:对给定的输入参数执行操作,返回一个boolean类型的结果(布尔值函数);boolean test(T t):根据给定的参数进行判断
1import java.util.Arrays; 2import java.util.List; 3import java.util.function.Predicate; 4 5/** 6 * 查找集合中包含字符“a”的元素 7 */ 8public class Lambda_Predicate { 9 private static List<String> list = Arrays.asList("java","PHP","China","World"); 10 public static void getContains(List<String> list, Predicate<String> predicate){ 11 for(String str : list){ 12 if(predicate.test(str)){ 13 System.out.println(str); 14 } 15 } 16 } 17 public static void main(String[] args) { 18 getContains(list,n->n.contains("a")); 19 } 20} 21
4. 基于BiPredicate函数式接口使用Lambda表达式
BiPredicate<T,U>:对给定的两个输入参数执行操作,返回一个boolean类型的结果(布尔值函数);boolean test(T t, U u):根据给定的两个输入参数进行判断
1import java.util.Arrays; 2import java.util.List; 3import java.util.function.BiPredicate; 4 5/** 6 * 判断集合中是否存在某个元素 7 */ 8public class Lambda_BiPredicate { 9 private static List<String> list = Arrays.asList("java","world"); 10 private static String name = "java"; 11 public static void IfContains(List<String> list , String name , BiPredicate<List<String>,String> biFunction){ 12 if(biFunction.test(list,name)){ 13 System.out.println("集合中存在该元素!"); 14 }else{ 15 System.out.println("集合中不存在该元素!"); 16 } 17 } 18 public static void main(String[] args) { 19 IfContains(list,name,(n,m) ->n.contains(m)); 20 } 21} 22
5. 基于Consumer函数式接口使用Lambda表达式
Consumer<T>:提供一个T类型的输入参数,不返回执行结果;void accept(T t):对给定的参数执行操作;default Consumer<T> andThen(Consumer<? super T> after):返回一个组合函数,after将会在该函数执行之后应用
1import java.util.function.Consumer; 2 3public class Lambda_Consumer { 4 public static void main(String[] args) { 5 /** 6 * void accept(T t) 7 * 对给定的参数执行操作 8 */ 9 Consumer<Integer> consumer1 = (t) -> { 10 System.out.println(t*3); 11 }; 12 consumer1.accept(8); 13 /** 14 * default Consumer<T> andThen(Consumer<? super T> after) 15 * 返回一个组合函数,after将会在该函数执行之后应用 16 */ 17 Consumer<Integer> consumer2 = (t) -> { 18 System.out.println(t*3); 19 }; 20 Consumer<Integer> consumerAfter = (s) -> { 21 System.out.println("之后执行:"+s); 22 }; 23 consumer2.andThen(consumerAfter).accept(5); 24 } 25 26} 27
6. 基于BiConsumer函数式接口使用Lambda表达式
BiConsumer<T,U>:提供两个自定义类型的输入参数,不返回执行结果;void accept(T t, U u):对给定的参数执行操作;default BiConsumer<T,U> andThen(BiConsumer<? super T,? super U> after):返回一个组合函数,after将会在该函数执行之后应用
1import java.util.function.BiConsumer; 2 3public class Lambda_BiConsumer { 4 5 public static void main(String[] args) { 6 /** 7 * void accept(T t, U u):对给定的参数执行操作; 8 */ 9 BiConsumer<String, String> biConsumer = (a, b) -> { 10 System.out.println(a.concat(b)); 11 }; 12 13 biConsumer.accept("Hello ", "World!"); 14 15 /** 16 * default BiConsumer<T,U> andThen(BiConsumer<? super T,? super U> after):返回一个组合函数,after将会在该函数执行之后应用 17 */ 18 BiConsumer<String, String> biConsumer1 = (a, b) -> { 19 System.out.println(a.concat(b)); 20 }; 21 biConsumer.andThen(biConsumer1).accept("Hello", " Java!"); 22 } 23} 24
7. 基于Supplier函数式接口使用Lambda表达式
Supplier<T>:不提供输入参数,但是返回结果的函数;T get() 获取结果值;
1import java.util.function.Supplier; 2 3public class Lambda_Supplier { 4 public static void main(String[] args) { 5 Supplier<String> supplier = () -> "Hello World!"; 6 System.out.println(supplier.get()); 7 } 8} 9
