为了应用不同的Lambda表达式,你需要一套能够描述常见函数描述符的函数式接口。Java API中已经提供了四大内置核心函数式接口
* JAVA8 内置的四大核心函数式接口
*
* Consumer :消费型接口
* void accept(T t);
* Supplier :供给型接口
* T get();
* Function :函数型接口
* R apply(T t);
* Predicate :断言型接口
* boolean test(T t);
一、Consumer消费型接口
接收一个参数类型T,无返回值void
对类型为T的对象应用操作,包含方法void accept(T t);
@FunctionalInterface
public interface Consumer{
void accept(T t);
//Consumer 消费型接口
@Test
public void test1(){
happy(1000,(m) -> System.out.println("今天出去玩,花了"+m+"元"));
}
public void happy(double money, Consumer con){
con.accept(money);
}
运行结果:
今天出去玩,花了1000.0元
二、Supplier供给型接口
参数类型无,返回类型T
返回类型为T的对象,包含方法T get();
@FunctionalInterface
public interface Supplier {
T get();
}
/**
* @author Dongguo
* @date 2021/8/14 0014 20:50
* @description:产生指定个数的整数,并放入集合
*/
public List getNumList(int num, Supplier s) {
List list = new ArrayList();
for (int i = 0; i (int) (Math.random() * 100));
for (Integer i:numList) {
System.out.println(i);
}
}
三、Function函数型接口
参数类型T ,返回类型R
对类型为T的对象应用操作,并返回结果。结果是R类型的对象。包含方法R apply(T t);
@FunctionalInterface
public interface Function {
R apply(T t);
}
/**
* @author Dongguo
* @date 2021/8/14 0014 21:01
* @description:用于处理字符串
*/
public String strHandler(String s , Function function){
return function.apply(s);
}
@Test
public void test3() {
String str= "abcdefg";
String s = strHandler(str, (x) -> x.toUpperCase());
System.out.println(s);
}
四、Predicate断定型接口
参数类型T,返回类型为boolean
确定类型为T的对象是否满足某约束条件,并返回boolean值。包含方法 boolean test(T t);
@FunctionalInterface
public interface Predicate {
boolean test(T t);
}
//Predicate :断言型接口
@Test
public void test4(){
List list = Arrays.asList("Consumer","Supplier","Function","Predicate","haha","hai");
List filterStr = filterStr(list, (s) -> s.length() > 3);
for (String str: filterStr){
System.out.println(str);
}
}
//需求: 将满足条件的字符串添加到集合中
public List filterStr(List list,Predicate pre){
List strList = new ArrayList();
for (String str: list) {
if (pre.test(str)){
strList.add(str);
}
}
return strList;
}
运行结果:
Consumer
Supplier
Function
Predicate
haha
其他扩展函数式接口
JAVA8 内置的四大核心函数式接口能够满足大多数情景下的需求,当然如果觉得这几个还是不够用,那么可以参考下面的一些拓展的接口,其用法与上面大同小异:
序号接口 & 描述1BiConsumer代表了一个接受两个输入参数的操作,并且不返回任何结果
2BiFunction代表了一个接受两个输入参数的方法,并且返回一个结果
3BinaryOperator代表了一个作用于于两个同类型操作符的操作,并且返回了操作符同类型的结果
4BiPredicate代表了一个两个参数的boolean值方法
5BooleanSupplier代表了boolean值结果的提供方
6Consumer代表了接受一个输入参数并且无返回的操作
7DoubleBinaryOperator代表了作用于两个double值操作符的操作,并且返回了一个double值的结果。
8DoubleConsumer代表一个接受double值参数的操作,并且不返回结果。
9DoubleFunction代表接受一个double值参数的方法,并且返回结果
10DoublePredicate代表一个拥有double值参数的boolean值方法
11DoubleSupplier代表一个double值结构的提供方
12DoubleToIntFunction接受一个double类型输入,返回一个int类型结果。
13DoubleToLongFunction接受一个double类型输入,返回一个long类型结果
14DoubleUnaryOperator接受一个参数同为类型double,返回值类型也为double 。
15Function接受一个输入参数,返回一个结果。
16IntBinaryOperator接受两个参数同为类型int,返回值类型也为int 。
17IntConsumer接受一个int类型的输入参数,无返回值 。
18IntFunction接受一个int类型输入参数,返回一个结果 。
19IntPredicate:接受一个int输入参数,返回一个布尔值的结果。
20IntSupplier无参数,返回一个int类型结果。
21IntToDoubleFunction接受一个int类型输入,返回一个double类型结果 。
22IntToLongFunction接受一个int类型输入,返回一个long类型结果。
23IntUnaryOperator接受一个参数同为类型int,返回值类型也为int 。
24LongBinaryOperator接受两个参数同为类型long,返回值类型也为long。
25LongConsumer接受一个long类型的输入参数,无返回值。
26LongFunction接受一个long类型输入参数,返回一个结果。
27LongPredicateR接受一个long输入参数,返回一个布尔值类型结果。
28LongSupplier无参数,返回一个结果long类型的值。
29LongToDoubleFunction接受一个long类型输入,返回一个double类型结果。
30LongToIntFunction接受一个long类型输入,返回一个int类型结果。
31LongUnaryOperator接受一个参数同为类型long,返回值类型也为long。
32ObjDoubleConsumer接受一个object类型和一个double类型的输入参数,无返回值。
33ObjIntConsumer接受一个object类型和一个int类型的输入参数,无返回值。
34ObjLongConsumer接受一个object类型和一个long类型的输入参数,无返回值。
35Predicate接受一个输入参数,返回一个布尔值结果。
36Supplier无参数,返回一个结果。
37ToDoubleBiFunction接受两个输入参数,返回一个double类型结果
38ToDoubleFunction接受一个输入参数,返回一个double类型结果
39ToIntBiFunction接受两个输入参数,返回一个int类型结果。
40ToIntFunction接受一个输入参数,返回一个int类型结果。
41ToLongBiFunction接受两个输入参数,返回一个long类型结果。
42ToLongFunction接受一个输入参数,返回一个long类型结果。
43UnaryOperator接受一个参数为类型T,返回值类型也为T。