镇江市扬中市做网站,wordpress cms主题vieu,如何开一个自己的网站,蝶恋花直播app下载安装背景 函数式接口很简单#xff0c;但是不是每一个函数式接口都需要我们自己来写jdk 根据 有无参数#xff0c;有无返回值#xff0c;参数的个数和类型#xff0c;返回值的类型 提前定义了一些通用的函数式接口 IntPredicate 参数#xff1a;有一个#xff0c;类型是int类…背景 函数式接口很简单但是不是每一个函数式接口都需要我们自己来写jdk 根据 有无参数有无返回值参数的个数和类型返回值的类型 提前定义了一些通用的函数式接口 IntPredicate 参数有一个类型是int类型返回值返回值是boolean类型 所以是Predicate IntPredicate type1_1 (int a) - (a 1) 0;FunctionalInterface
interface MyInterface1{boolean op(int arg1);
}IntBinaryOperator 参数两个int 所以是Binary返回值和参数一样是int 所以是Operator IntBinaryOperator type4_1 (int a, int b) - a b;
IntBinaryOperator type5_1 (int a, int b) - a * b;FunctionalInterface
interface MyInterface3{int op(int num1, int num2);
}Supplier 参数无返回值没有参数但是有返回值所以是Supplier SupplierStudent type6_2 () - new Student(张三, 18);SupplierListStudent type7_2 () - {ListStudent list new ArrayList();list.add(new Student(张三, 18));list.add(new Student(李四, 19));return list;};// MyInterface5和MyInterface4是可以使用泛型合并的
FunctionalInterface
interface MyInterface5_1T{T op() ;
}Function 有参数也有返回值但是参数类型和返回值类型不一样所以是Function FunctionStudent,String type8_2 (Student student) - student.getName();
FunctionStudent,Integer type9_2 (Student student) - student.getage();//MyInterface6和MyInterface7是可以使用泛型合并的
// O是返回值类型I是入参类型
FunctionalInterface
interface MyInterface8I,O{O op(I inObj) ;
}常见的函数式接口 命名规则 练习 获取list中的偶数数据返回的是Boolean类型的结果使用进行Predicate接口接受 public static void main(String[] args) {ListInteger list new ArrayList();for (int i 0; i 10; i) {list.add(i);}// 获取偶数ListInteger evenNumbers filter(list, i - i % 2 0);System.out.println(evenNumbers);}// 传递两个参数一个列表和一个 Predicate 接口对列表进行过滤static ListInteger filter(ListInteger list, PredicateInteger predicate) {ListInteger result new ArrayList();for (Integer i : list) {// 使用 Predicate 的 test() 方法判断元素是否满足条件 if (predicate.test(i)) {result.add(i);}}return result;}