从 java 8 开始便出现了 函数式接口 (functional interface,以下简称fi)
定义为: 如果一个接口只有唯一的一个抽象接口,则称之为函数式接口。为了保证接口符合 fi ,通常会在接口类上添加 @functionalinterface 注解。理解了函数式接口可以为 java 函数式编程打下基础,最终可通过运用函数式编程极大地提高编程效率。
函数式接口 (functional interface) 就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。
函数式接口可以对现有的函数友好地支持 lambda。
jdk 1.8 之前已有的函数式接口:
java.lang.runnable java.util.concurrent.callable java.security.privilegedaction java.util.comparator java.io.filefilter java.nio.file.pathmatcher java.lang.reflect.invocationhandler java.beans.propertychangelistener java.awt.event.actionlistener javax.swing.event.changelistenerjdk 1.8 新增加的函数接口:
java.util.function网上很多教程说新增 4 个函数接口是不对的,java.util.function 它包含了很多类,用来支持 java的 函数式编程,该包中的函数式接口 43 个,但是最主要的是这四个:
(1)功能性接口:function<t,r>
(2)断言性接口:predicate<t>
(3)供给性接口:supplier<t>
(4)消费性接口:consumer<t>
详细一点介绍:
consumer | t | void | 对类型t参数操作,无返回结果,包含方法 void accept(t t) |
supplier | 无 | t | 返回t类型参数,方法时 t get() |
function | t | r | 对类型t参数操作,返回r类型参数,包含方法 r apply(t t) |
predicate | t | boolean | 断言型接口,对类型t进行条件筛选操作,返回boolean,包含方法 boolean test(t t) |
具体的使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
/** * java8内置的四大核心函数式接口: * consumer<t>:消费型接口</t> * supplier<t>供给型接口</t> * function<t,r>函数型接口</t,r> * predicate<t>段言型接口</t> * boolean test(t t) */
public class testlamda3 {
//consumer<t> @test public void test1(){ happy( 10000 ,(m)-> system.out.println( "这次消费了" +m+ "元" )); }
public void happy( double money, consumer< double > con){ con.accept(money); }
//supplier<t> @test public void test2(){ list<integer> list= getnumlist( 5 ,()->{ return ( int )math.random()* 100 ; }); list.foreach(system.out::println); }
public list<integer> getnumlist( int num, supplier<integer> supplier){ list<integer> list= new arraylist<>(); for ( int i= 0 ; i<num;i++){ integer n=supplier.get(); list.add(n); } return list; }
//函数式接口 @test public void test4(){ string newstr=strhandle( "\t\t\t woshi nide " ,(str)->str.trim()); system.out.println(newstr); }
public string strhandle(string str,function<string,string> fun){ return fun.apply(str); }
//段言型接口;将满足条件的字符串放入集合中 @test public void test5(){ list<string> list1= arrays.aslist( "nihao" , "hiehei" , "woai" , "ni" ); list<string> list=filterstr(list1,(s)->s.length()> 3 ); for (string s : list) { system.out.println(s); } } public list<string> filterstr(list<string> list, predicate<string> pre){ list<string> strings= new arraylist<>(); for (string string : list) { if (pre.test(string)){ strings.add(string); } } return strings; } } |
全部接口:
1 |
biconsumer<t,u>
代表了一个接受两个输入参数的操作,并且不返回任何结果 |
2 |
bifunction<t,u,r>
代表了一个接受两个输入参数的方法,并且返回一个结果 |
3 |
binaryoperator<t>
代表了一个作用于于两个同类型操作符的操作,并且返回了操作符同类型的结果 |
4 |
bipredicate<t,u>
代表了一个两个参数的boolean值方法 |
5 |
booleansupplier
代表了boolean值结果的提供方 |
6 |
consumer<t>
代表了接受一个输入参数并且无返回的操作 |
7 |
doublebinaryoperator
代表了作用于两个double值操作符的操作,并且返回了一个double值的结果。 |
8 |
doubleconsumer
代表一个接受double值参数的操作,并且不返回结果。 |
9 |
doublefunction<r>
代表接受一个double值参数的方法,并且返回结果 |
10 |
doublepredicate
代表一个拥有double值参数的boolean值方法 |
11 |
doublesupplier
代表一个double值结构的提供方 |
12 |
doubletointfunction
接受一个double类型输入,返回一个int类型结果。 |
13 |
doubletolongfunction
接受一个double类型输入,返回一个long类型结果 |
14 |
doubleunaryoperator
接受一个参数同为类型double,返回值类型也为double 。 |
15 |
function<t,r>
接受一个输入参数,返回一个结果。 |
16 |
intbinaryoperator
接受两个参数同为类型int,返回值类型也为int 。 |
17 |
intconsumer
接受一个int类型的输入参数,无返回值 。 |
18 |
intfunction<r>
接受一个int类型输入参数,返回一个结果 。 |
19 |
intpredicate
接受一个int输入参数,返回一个布尔值的结果。 |
20 |
intsupplier
无参数,返回一个int类型结果。 |
21 |
inttodoublefunction
接受一个int类型输入,返回一个double类型结果 。 |
22 |
inttolongfunction
接受一个int类型输入,返回一个long类型结果。 |
23 |
intunaryoperator
接受一个参数同为类型int,返回值类型也为int 。 |
24 |
longbinaryoperator
接受两个参数同为类型long,返回值类型也为long。 |
25 |
longconsumer
接受一个long类型的输入参数,无返回值。 |
26 |
longfunction<r>
接受一个long类型输入参数,返回一个结果。 |
27 |
longpredicate
r接受一个long输入参数,返回一个布尔值类型结果。 |
28 |
longsupplier
无参数,返回一个结果long类型的值。 |
29 |
longtodoublefunction
接受一个long类型输入,返回一个double类型结果。 |
30 |
longtointfunction
接受一个long类型输入,返回一个int类型结果。 |
31 |
longunaryoperator
接受一个参数同为类型long,返回值类型也为long。 |
32 |
objdoubleconsumer<t>
接受一个object类型和一个double类型的输入参数,无返回值。 |
33 |
objintconsumer<t>
接受一个object类型和一个int类型的输入参数,无返回值。 |
34 |
objlongconsumer<t>
接受一个object类型和一个long类型的输入参数,无返回值。 |
35 |
predicate<t>
接受一个输入参数,返回一个布尔值结果。 |
36 |
supplier<t>
无参数,返回一个结果。 |
37 |
todoublebifunction<t,u>
接受两个输入参数,返回一个double类型结果 |
38 |
todoublefunction<t>
接受一个输入参数,返回一个double类型结果 |
39 |
tointbifunction<t,u>
接受两个输入参数,返回一个int类型结果。 |
40 |
tointfunction<t>
接受一个输入参数,返回一个int类型结果。 |
41 |
tolongbifunction<t,u>
接受两个输入参数,返回一个long类型结果。 |
42 |
tolongfunction<t>
接受一个输入参数,返回一个long类型结果。 |
43 |
unaryoperator<t>
接受一个参数为类型t,返回值类型也为t。 |
总结
函数式接口 (functional interface) 就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。
函数式接口是为了 lambda 表达式服务,函数式接口的存在是 lambda 表达式出现的前提,lambda 表达式想关于重写了函数式接口中的唯一方法。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/qq_40147863/article/details/85085153
查看更多关于浅谈Java 8 新增函数式接口到底是什么的详细内容...