java自定义函数式接口

⾃定义lambda接⼝流程
  • 定义⼀个函数式接⼝ 需要标注此接⼝ @FunctionalInterface,否则万⼀团队成员在接⼝上加
    了其他⽅法则容易出故障
  • 编写⼀个⽅法,输⼊需要操做的数据和接⼝
  • 在调⽤⽅法时传⼊数据 和 lambda 表达式,⽤来操作数据

需求,定义⼀个可以使⽤加减乘除的接⼝ 以前需要定义4个⽅法
使⽤Lambda表达式后

@FunctionalInterface
public interface OperFunction<R,T> {
 R operator(T t1, T t2);
}
public static void main(String[] args) throws Exception {
 System.out.println(operator(20, 5, (Integer x, Integer y) -> {
 return x * y;
 }));
 System.out.println(operator(20, 5, (x, y) -> x + y));
 System.out.println(operator(20, 5, (x, y) -> x - y));
 System.out.println(operator(20, 5, (x, y) -> x / y));
 }
 public static Integer operator(Integer x, Integer y,
OperFunction<Integer, Integer> of) {
 return of.operator(x, y);
 }

java 内置的函数式接口
Java8 内置的四⼤核⼼函数式接⼝

    Consumer<T> : 消费型接⼝:有⼊参,⽆返回值
     void accept(T t);
     
    Supplier<T> : 供给型接⼝:⽆⼊参,有返回值
     T get();
     
    Function<T, R> : 函数型接⼝:有⼊参,有返回值
     R apply(T t);
     
    Predicate<T> : 断⾔型接⼝:有⼊参,有返回值,返回值类型确定是boolean
     boolean test(T t);
  1. Function
    • 传⼊⼀个值经过函数的计算返回另⼀个值
    • T:⼊参类型,R:出参类型
      调⽤⽅法:R apply(T t)
@FunctionalInterface
public interface Function<T, R> {
 /**
 * Applies this function to the given argument.
 *
 * @param t the function argument
 * @return the function result
 */
 R apply(T t);
}

  • 作⽤:将转换逻辑提取出来,解耦合
  • 不要看过于复杂,就是⼀个接⼝,下⾯是⾃定义实现
public class FunctionObj implements Function {
 @Override
 public Object apply(Object o) {
 return o+"经过apply处理拼接上了";
 }
}
  • 常规使用
// 输出⼊参的10倍
Function<Integer, Integer> func = p -> p * 100; 
func.apply(100);
  1. BiFunction
  • Function只能接收⼀个参数,如果要传递两个参数,则⽤ BiFunction
@FunctionalInterface
public interface BiFunction<T, U, R> {
 R apply(T t, U u);
}

  • 需求: 上两节课,两个数的四则运算
public static void main(String[] args) {
 System.out.println(operator(10,21,(a,b)->a+b));
 System.out.println(operator(10,2,(a,b)->a-b));
 System.out.println(operator(8,4,(a,b)->a*b));
 System.out.println(operator(10,2,(a,b)->a/b));
 }
 public static Integer operator(Integer a, Integer b, BiFunction<Integer,
Integer, Integer> bf) {
 return bf.apply(a, b);
 }
  1. Consumer
  • Consumer 消费型接⼝:有⼊参,⽆返回值
  • 将 T 作为输⼊,不返回任何内容
    调⽤⽅法:void accept(T t);
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
  • ⽤途: 因为没有出参,常⽤于打印、发送短信等消费动作
public static void main(String[] args) throws Exception {
 Consumer<String> consumer = obj->{
 System.out.println(obj);
 System.out.println("调⽤短信接⼝发送短信,或者打印⽇志");
 };
// sendMsg("8888888",obj->{
// System.out.println(obj);
// System.out.println("调⽤短信接⼝发送短信,或者打印⽇志");
// });
 sendMsg("8888888",consumer);
 }
 public static void sendMsg(String phone,Consumer<String> consumer){
 consumer.accept(phone);
 }
  • 典型应⽤,集合的foreach
List<String> list = Arrays.asList("aaa","bbb");
list.forEach(obj->{
//TODO
});
  1. Supplier
  • Supplier: 供给型接⼝:⽆⼊参,有返回值
  • T:出参类型;没有⼊参
    调⽤⽅法:T get();
@FunctionalInterface
public interface Supplier<T> {
T get();
}
  • ⽤途: 泛型⼀定和⽅法的返回值类型是⼀种类型,如果需要获得⼀个数据,并且不需要传⼊参数,可以使⽤Supplier接⼝,例如 ⽆参的⼯⼚⽅法,即⼯⼚设计模式创建对象,简单来说就是 提供者
public static void main(String[] args) {
 //Student student = new Student();
 Student student = newStudent();
 System.out.println(student.getName());
 }
 public static Student newStudent(){
 Supplier<Student> supplier = ()-> {
 Student student = new Student();
student.setName("默认名称");
 return student;
 };
 return supplier.get();
 }
 
 class Student{
 private String name;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 }
  1. Predicate
  • Predicate: 断⾔型接⼝:有⼊参,有返回值,返回值类型确定是boolean
  • T:⼊参类型;出参类型是Boolean
    调⽤⽅法:boolean test(T t);
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
  • ⽤途: 接收⼀个参数,⽤于判断是否满⾜⼀定的条件,过滤数据
public static void main(String[] args) {
 List<String> list =
Arrays.asList("awewrwe","vdssdsd","aoooo","psdddsd");
List<String> results = filter(list,obj->obj.startsWith("a"));
 System.out.println(results);
 }
 public static List<String> filter(List<String> list,
Predicate<String> predicate) {
 List<String> results = new ArrayList<>();
 for (String str : list) {
 if (predicate.test(str)) {
 results.add(str);
 }
 }
 return results;
 }
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ITzhongzi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值