为了应用不同的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 内置的四大核心函数式接口能够满足大多数情景下的需求,当然如果觉得这几个还是不够用,那么可以参考下面的一些拓展的接口,其用法与上面大同小异:
| 序号 | 接口 & 描述 |
|---|---|
| 1 | BiConsumer 代表了一个接受两个输入参数的操作,并且不返回任何结果 |
| 2 | BiFunction 代表了一个接受两个输入参数的方法,并且返回一个结果 |
| 3 | BinaryOperator 代表了一个作用于于两个同类型操作符的操作,并且返回了操作符同类型的结果 |
| 4 | BiPredicate 代表了一个两个参数的boolean值方法 |
| 5 | BooleanSupplier 代表了boolean值结果的提供方 |
| 6 | Consumer 代表了接受一个输入参数并且无返回的操作 |
| 7 | DoubleBinaryOperator 代表了作用于两个double值操作符的操作,并且返回了一个double值的结果。 |
| 8 | DoubleConsumer 代表一个接受double值参数的操作,并且不返回结果。 |
| 9 | DoubleFunction 代表接受一个double值参数的方法,并且返回结果 |
| 10 | DoublePredicate 代表一个拥有double值参数的boolean值方法 |
| 11 | DoubleSupplier 代表一个double值结构的提供方 |
| 12 | DoubleToIntFunction 接受一个double类型输入,返回一个int类型结果。 |
| 13 | DoubleToLongFunction 接受一个double类型输入,返回一个long类型结果 |
| 14 | DoubleUnaryOperator 接受一个参数同为类型double,返回值类型也为double 。 |
| 15 | Function 接受一个输入参数,返回一个结果。 |
| 16 | IntBinaryOperator 接受两个参数同为类型int,返回值类型也为int 。 |
| 17 | IntConsumer 接受一个int类型的输入参数,无返回值 。 |
| 18 | IntFunction 接受一个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 接受一个long类型输入参数,返回一个结果。 |
| 27 | LongPredicate R接受一个long输入参数,返回一个布尔值类型结果。 |
| 28 | LongSupplier 无参数,返回一个结果long类型的值。 |
| 29 | LongToDoubleFunction 接受一个long类型输入,返回一个double类型结果。 |
| 30 | LongToIntFunction 接受一个long类型输入,返回一个int类型结果。 |
| 31 | LongUnaryOperator 接受一个参数同为类型long,返回值类型也为long。 |
| 32 | ObjDoubleConsumer 接受一个object类型和一个double类型的输入参数,无返回值。 |
| 33 | ObjIntConsumer 接受一个object类型和一个int类型的输入参数,无返回值。 |
| 34 | ObjLongConsumer 接受一个object类型和一个long类型的输入参数,无返回值。 |
| 35 | Predicate 接受一个输入参数,返回一个布尔值结果。 |
| 36 | Supplier 无参数,返回一个结果。 |
| 37 | ToDoubleBiFunction 接受两个输入参数,返回一个double类型结果 |
| 38 | ToDoubleFunction 接受一个输入参数,返回一个double类型结果 |
| 39 | ToIntBiFunction 接受两个输入参数,返回一个int类型结果。 |
| 40 | ToIntFunction 接受一个输入参数,返回一个int类型结果。 |
| 41 | ToLongBiFunction 接受两个输入参数,返回一个long类型结果。 |
| 42 | ToLongFunction 接受一个输入参数,返回一个long类型结果。 |
| 43 | UnaryOperator 接受一个参数为类型T,返回值类型也为T。 |
