外贸网站 源码,做的好详情页网站,高唐做网站建设的公司,wordpress 文档中心这里写自定义目录标题 一、什么是函数式接口二、自定义函数式接口三、作为参数传递 Lambda 表达式四、四大内置核心函数式接口1、消费形接口2、供给形接口3、函数型接口4、断言形接口 一、什么是函数式接口
只包含一个抽象方法的接口#xff0c;称为函数式接口。你可以通过 L… 这里写自定义目录标题 一、什么是函数式接口二、自定义函数式接口三、作为参数传递 Lambda 表达式四、四大内置核心函数式接口1、消费形接口2、供给形接口3、函数型接口4、断言形接口 一、什么是函数式接口
只包含一个抽象方法的接口称为函数式接口。你可以通过 Lambda 表达式来创建该接口的对象。我们可以在任意函数式接口上使用 FunctionalInterface 注解这样做可以检查它是否是一个函数式接口同时 javadoc 也会包含一条声明说明这个接口是一个函数式接口
二、自定义函数式接口
FunctionalInterface
public interface MyFunction {public String getValue(String str);
}FunctionalInterface
public interface MyFuncT {public R getValue(T t);
}三、作为参数传递 Lambda 表达式
FunctionalInterface
public interface MyFuncT {public T getValue(T t);
}public String toUpperString(MyFuncString mf, String str) {mf.getValue(str)
}//Lambda 表达式作为参数传递
String str1 toUpperString(str - str.toUpperCase(),abcd);
sout(str1 )四、四大内置核心函数式接口 1、消费形接口 //Consumer: 消费型接口Testpublic void test1(){happy(35,d - System.out.println(花费了d));}public void happy(double money, ConsumerDouble con){con.accept(money);}2、供给形接口 Testpublic void test2(){ListInteger numberList getNumberList(10, () - (int) (Math.random() * 100));for(Integer num : numberList){System.out.println(num);}}//需求产生一些整数并放在集合中public ListInteger getNumberList(int num, SupplierInteger sup){ListInteger list new ArrayList();for(int i 0; i num ; i){list.add(sup.get());}return list;}3、函数型接口 Testpublic void test3(){String result Strandler(\t\thello world,i like china!, str - str.trim());System.out.println(result);}//需求用于处理字符串public String Strandler(String str, FunctionString,String fun){return fun.apply(str);}4、断言形接口 Testpublic void test4(){ListString employees Arrays.asList(hello,houchen,shuchenxi);ListString list filterStr(employees, str - str.length() 3);for(String s : list){System.out.println(s);}}//需求将满足条件的字符串放入集合中public ListString filterStr(ListString list, PredicateString pre){ListString stringList new ArrayList();for(String str : list){if(pre.test(str)){stringList.add(str);}}return stringList;}