首页 > 其他分享 >函数式接口

函数式接口

时间:2022-10-28 14:59:16浏览次数:48  
标签:Predicate run 函数 接口 interface public

函数式接口,Functional Interface 是一个有且仅有一个的抽象方法,可以有多个非抽象方法的接口

函数式接口可以被隐式的转换为lambda表达式

1.定义

@FunctionalInterface
interface GreetingService {
	void sayMessage(String message);
}

可以用lambda表达式,表示这个GreetingService接口的实现

GreetingService greetService = message -> System.out.println("hello" + message)

2. java8已有的函数式接口

Runnable接口

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

Callable接口

@FunctionalInterface
public interface Callable<V> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    V call() throws Exception;
}

等等

3.函数式接口示例

Predicate接口是一个函数式接口,接受参数T,返回一个布尔值结果
包含多种默认方法,将Predicate组合成其他复杂逻辑

// 定义一个返回Predicate接口的函数,jdk8以前用的是匿名内部类方式,函数里用的是lambda表达式
public Predicate<NotificationContext> isRdsResourcePredicate(){
        return c -> delta.api.enums.ResourceType.RDS.getInnerResTypeSet().contains(c.resourceType.getSourceName());
    }
	
	// 定义方法判断是否是rds资源
    public boolean isRdsResource(NotificationContext context) {
        return isRdsResourcePredicate().test(context);
    }	

标签:Predicate,run,函数,接口,interface,public
From: https://www.cnblogs.com/PythonOrg/p/16836055.html

相关文章

  • 递归函数
    概念:需要满足两个条件1.引用自身 2.有一个最小可能性返回值1#n的阶乘2#方法一(常规)3deffactorial(n):4result=15foriinrange(1,n+1):......
  • JavaScript 箭头函数
    箭头函数的形式:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metahttp-equiv="X-UA-Compatible"content="IE=edge"/><metana......
  • Java8 新特性01-接口修饰-Lambda
    Java8新特性文章目录​​Java8新特性​​​​接口中默认的方法修饰为普通方法​​​​Lambda表达式​​​​为什么要使用Lambda表达式​​​​Lambda表达式的规范​​​​......
  • 一个函数抽取类壳的脱壳过程
    AndroidKiller查看此apk发现入口Activity找不到,说明此apk被加壳了。接着查看apk反编译后的代码,发现使用的XX的乐固。首先尝试在DEX文件加载过程中进行hook,例如hookDex......
  • javaSE10--final关键字-抽象类-接口-抽象类接口区别
    final关键字final用于声明属性,方法和类,可以修饰类,修饰字段,修饰方法,修饰局部变量,修饰形参属性:定义就必须直接赋值或者在构造方法中进行赋值,并且后期不能修改,修饰修......
  • C++——指针作为函数参数传递需要注意的问题
    指针作为函数参数传递的一些问题总结:#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<iostream>#include<cstdlib>#include<cstring>usingnamespace......
  • 久违了!欧拉函数
    GCD-HDU2588-VirtualJudge(vjudge.net)题意:给定n,m,n<=1e9for(inti=1;i<=n;i++){if(gcd(i,n)>=m)cnt++;}注意到n<=1e9,这个模拟算法是不能通过的如......
  • Oracle 数据库 日常操作函数
    --获取当天数据select*fromPOS_TRANMAINwheretrunc(SLDATE)=trunc(sysdate);--decode等同于casewhenthendecode(条件,值1,返回值1,值2,返回值2....)--to_char......
  • L:python基础知识:数学运算函数,字符串内置函数,math数学模块,random随机模块,datetim
    数学运算函数asb(x)返回绝对值divmod(x,y)返回一个(x//y,x%y)的元组,可以用两个变量来接收它max(seq),min(seq)返回seq序列的最大值,最小值pow(x,y)返回x的y次方round(x,p......
  • 数论-欧拉函数 学习笔记
    一、欧拉函数1.欧拉函数的定义欧拉函数(Euler’stotientfunction),即,表示的是小于等于和比如说。当n是质数的时候,显然有。2.欧拉函数的一些性质欧拉函数是积性函数。......