当前位置: 代码网 > it编程>编程语言>Java > Java8中Function函数式接口用法及说明

Java8中Function函数式接口用法及说明

2026年04月30日 Java 我要评论
1.函数式接口函数式接口(functional interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。functional interface(功能接口)为lambda表达

1.函数式接口

函数式接口(functional interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。

functional interface(功能接口)为lambda表达式和方法引用(用冒号::来进行方法的调用)提供目标类型。每个功能接口都有一个抽象方法,称为该功能接口的功能方法,lambda表达式的参数和返回类型与之匹配或适配。功能接口可以在多个上下文中提供目标类型,例如赋值上下文,方法调用或强制转换上下文:

// assignment context
predicate<string> p = string::isempty;
// method invocation context
stream.filter(e -> e.getsize() > 10)...
// cast context
stream.map((tointfunction) e -> e.getsize())...

函数式接口可以使用lambda表达式,方法引用或构造函数引用创建功能接口的实例。

java8为函数式接口引入了一个新注解@functionalinterface,主要用于编译级错误检查,加上该注解,当接口不符合函数式接口定义的时候,编译器会报错。

此注解不是编译器将接口识别为功能接口的必要条件,而仅是帮助捕获设计意图并获得编译器帮助识别意外违反设计意图的帮助。

正确例子,没有报错:

@functionalinterface
public interface helloworldservice {
    void sayhello(string msg);
}

错误例子,接口中包含了两个抽象方法,违反了函数式接口的定义,提示在接口中找到多个非重写的抽象方法。

注意: 加不加 @functionalinterface对于接口是不是函数式接口没有影响,该注解只是提醒编译器去检查该接口是否仅包含一个抽象方法。

1.1允许定义默认方法

函数式接口里是可以包含默认方法,因为默认方法不是抽象方法,其有一个默认实现,所以是符合函数式接口的定义的。

如下代码不会报错:

@functionalinterface
public interface helloworldservice {
    void sayhello(string msg);
    default void dosomework1() {
        // method body
    }
    default void dosomework2() {
        // method body
    }
}

1.2允许定义静态方法

函数式接口里是可以包含静态方法,因为静态方法不能是抽象方法,是一个已经实现了的方法,所以是符合函数式接口的定义的。

如下代码不会报错:

@functionalinterface
public interface helloworldservice {
    void sayhello(string msg);
    static void printhello() {
        system.out.println("hello");
    }
}

1.3允许定义java.lang.object的public方法

函数式接口里是可以包含object里的public方法,这些方法对于函数式接口来说,不被当成是抽象方法(虽然它们是抽象方法);因为任何一个函数式接口的实现,默认都继承了object类,包含了来自java.lang.object里对这些抽象方法的实现;

如下代码不会报错:

@functionalinterface
public interface helloworldservice {
    void sayhello(string msg);
	@override
    boolean equals(object obj);
}

1.4已有函数式接口

函数式接口可以对现有的函数友好地支持lambda。

jdk1.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.changelistener

jdk1.8新增加的函数接口:

  • java.util.function

2.function函数

序号接口描述
1function<t,r>接收一个参数并返回结果的函数
2bifunction<t,u,r>接受两个参数并返回结果的函数
3doublefunction<r>接收一个double类型的参数并返回结果的函数
4doubletointfunction接收一个double类型的参数并返回int结果的函数
5doubletolongfunction接收一个double类型的参数并返回long结果的函数
6intfunction<r>接收一个int类型的参数并返回结果的函数
7inttodoublefunction接收一个int类型的参数并返回double结果的函数
8inttolongfunction接收一个int类型的参数并返回long结果的函数
9longfunction<r>接收一个long类型的参数并返回结果的函数
10longtodoublefunction接收一个long类型的参数并返回double结果的函数
11longtointfunction接收一个long类型的参数并返回int结果的函数
12todoublebifunction<t,u>接收两个参数并返回double结果的函数
13todoublefunction<t>接收一个参数并返回double结果的函数
14tointbifunction<t,u>接收两个参数并返回int结果的函数
15tointfunction<t>接收一个参数并返回int结果的函数
16tolongbifunction<t,u>接收两个参数并返回long结果的函数
17tolongfunction<t>接收一个参数并返回long结果的函数

2.1function<t, r>

接口方法方法描述
r apply(t t)将此参数应用到函数中
function<t, r> andthen(function<? super r,? extends v> after)返回一个组合函数,该函数结果应用到after函数中
function<t, r> compose(function<? super v,? extends t> before)返回一个组合函数,首先将入参应用到before函数,再将before函数结果应用到该函数中

①apply(t t)

function<string, string> function = a -> a +" jack!";
system.out.println(function.apply("hello")); // hello jack!

②andthen(function<? super r,? extends v> after)

function<string, string> function = a -> a +" jack!";
function<string, string> function1 = a -> a + " bob!";
string greet = function.andthen(function1).apply("hello");
system.out.println(greet); // hello jack! bob!

③compose(function<? super v,? extends t> before)

function<string, string> function = a -> a +" jack!";
function<string, string> function1 = a -> a + " bob!";
string greet = function.compose(function1).apply("hello");
system.out.println(greet); // hello bob! jack!

2.2bifunction<t, u, r>

接口方法方法描述
r apply(t t, u u)将参数应用于函数执行
bifunction<t,u,v> andthen(function<? super r,? extends v> after)返回一个组合函数,after函数应用于该函数之后

①apply(t t, u u)

bifunction<string, string, string> bifunction = (a, b) -> a + b;
system.out.println(bifunction.apply("hello ", "jack!")); // hello jack!

②andthen(function<? super r,? extends v> after)

bifunction<string, string, string> bifunction = (a, b) -> a + b;
function<string, string> function = (a) -> a +"!!!";
system.out.println(bifunction.andthen(function).apply("hello", " jack")); // hello jack!!!

2.3doublefunction

接口方法方法描述
r apply(double value)根据给定参数执行函数

①apply(double value)

doublefunction<string> doublefunction = doub ->"结果:" + doub;
system.out.println(doublefunction.apply(1.6)); // 结果:1.6

2.4doubletointfunction

接口方法方法描述
int applyasint(double value)根据给定的参数执行函数

①applyasint(double value)

doubletointfunction doubletointfunction = doub -> double.valueof(doub).intvalue();
system.out.println(doubletointfunction.applyasint(1.2)); // 1

2.5todoublebifunction<t,u>

接口方法方法描述
double applyasdouble(t t, u u)根据给定的参数执行函数

①applyasdouble(t t, u u)

todoublebifunction<long, float> todoublebifunction = (lon, floa) -> lon
	.doublevalue() + floa.doublevalue();
system.out.println(todoublebifunction.applyasdouble(11l, 235.5f)); // 246.5

2.6todoublefunction

接口方法方法描述
double applyasdouble(t value)根据给定参数执行函数

①applyasdouble(t value)

todoublefunction<float> todoublefunction = floa -> floa.doublevalue();
system.out.println(todoublefunction.applyasdouble(12315f)); // 12315.0

3.consumer消费者

序号接口描述
1consumer<t>提供一个t类型的输入参数,不返回执行结果
2biconsumer<t,u>提供两个自定义类型的输入参数,不返回执行结果
3doubleconsumer表示接受单个double值参数,但不返回结果的操作
4intconsumer表示接受单个int值参数,但不返回结果的操作
5longconsumer表示接受单个long值参数,但不返回结果的操作
6objdoubleconsumer<t>表示接受object值和double值,但是不返回任何操作结果
7objintconsumer<t>表示接受object值和int值,但是不返回任何操作结果
8objlongconsumer<t>表示接受object值和long值,但是不返回任何操作结果

3.1consumer<t>

接口方法方法描述
void accept(t t)对给定的参数执行操作
default consumer andthen(consumer<? super t> after)返回一个组合函数,after将会在该函数执行之后应用

①accept(t t)

stringbuilder sb = new stringbuilder("hello ");
consumer<stringbuilder> consumer = (str) -> str.append("jack!");
consumer.accept(sb);
system.out.println(sb.tostring());	// hello jack!

②andthen(consumer<? super t> after)

stringbuilder sb = new stringbuilder("hello ");
consumer<stringbuilder> consumer = (str) -> str.append("jack!");
consumer<stringbuilder> consumer1 = (str) -> str.append(" bob!");
consumer.andthen(consumer1).accept(sb);
system.out.println(sb.tostring());	// hello jack! bob!

3.2biconsumer<t,u>

接口方法方法描述
void accept(t t, u u)对给定的参数执行操作
default biconsumer<t,u> andthen(biconsumer<? super t,? super u> after)返回一个组合函数,after将会在该函数执行之后应用

①accept(t t, u u)

stringbuilder sb = new stringbuilder();
biconsumer<string, string> biconsumer = (a, b) -> {
	sb.append(a);
	sb.append(b);
};
biconsumer.accept("hello ", "jack!");
system.out.println(sb.tostring());	// hello jack!

②andthen(biconsumer<? super t,? super u> after)

stringbuilder sb = new stringbuilder();
biconsumer<string, string> biconsumer = (a, b) -> {
	sb.append(a);
	sb.append(b);
};
biconsumer<string, string> biconsumer1 = (a, b) -> {
	system.out.println(a + b);
};
biconsumer.andthen(biconsumer1).accept("hello", " jack!"); // hello jack!

3.3doubleconsumer

接口方法方法描述
void accept(double value)对给定的参数执行操作
default doubleconsumer andthen(doubleconsumer after)返回一个组合函数,after在该函数执行之后应用

①accept(double value)

doubleconsumer doubleconsumer = system.out::println;
doubleconsumer.accept(1.3); // 1.3

②andthen(doubleconsumer after)

doubleconsumer doubleconsumer = system.out::println;
doubleconsumer doubleconsumer1 = system.out::println;
doubleconsumer.andthen(doubleconsumer1).accept(1.3);
// 1.3  
// 1.3

3.4objdoubleconsumer<t>

接口方法方法描述
void accept(t t, double value)对给定的参数执行操作

①accept(t t, double value)

objdoubleconsumer<string> doubleconsumer = (obj, doub)
	-> system.out.println(obj + doub);
doubleconsumer.accept("金额:", 222.66); // 金额:222.66

4.predicate谓语

序号接口描述
1predicate<t>对给定的输入参数执行操作,返回一个boolean类型的结果(布尔值函数)
2bipredicate<t,u>对给定的两个输入参数执行操作,返回一个boolean类型的结果(布尔值函数)
3doublepredicate对给定的double参数执行操作,返回一个boolean类型的结果(布尔值函数)
4intpredicate对给定的int输入参数执行操作,返回一个boolean类型的结果(布尔值函数)
5longpredicate对给定的long参数执行操作,返回一个boolean类型的结果(布尔值函数)

4.1predicate<t>

接口方法方法描述
boolean test(t t)根据给定的参数进行判断
predicate and(predicate<? super t> other)返回一个组合判断,将other以短路与的方式加入到函数的判断中
predicate or(predicate<? super t> other)返回一个组合判断,将other以短路或的方式加入到函数的判断中
predicate negate()将函数的判断取反

①test(t t)

predicate<integer> predicate = number -> number !=0;
system.out.println(predicate.test(10));    //true

②and(predicate<? super t> other)

predicate<integer> predicate = number -> number !=0;
predicate = predicate.and(number -> number >= 10);
system.out.println(predicate.test(10));    //true

③or(predicate<? super t> other)

predicate<integer> predicate = number -> number !=0;
predicate = predicate.or(number -> number != 10);
system.out.println(predicate.test(10));    //true

④negate()

predicate<integer> predicate = number -> number !=0;
predicate = predicate.negate();
system.out.println(predicate.test(10));    //false

4.2bipredicate<t,u>

接口方法方法描述
boolean test(t t, u u)根据给定的两个输入参数进行判断
bipredicate<t,u> and(bipredicate<? super t,? super u> other)返回一个组合判断,将other以短路与的方式加入到函数的判断中
bipredicate<t,u> or(bipredicate<? super t,? super u> other)返回一个组合判断,将other以短路或的方式加入到函数的判断中
bipredicate<t,u> negate()将函数的判断取反

①test(t t, u u)

bipredicate<integer, integer> bipredicate = (a, b) -> a != b;
system.out.println(bipredicate.test(1, 2)); // true

②and(bipredicate<? super t,? super u> other)

bipredicate<integer, integer> bipredicate = (a, b) -> a != b;
bipredicate = bipredicate.and((a, b) -> a.equals(b));
system.out.println(bipredicate.test(1, 2)); // false

③or(bipredicate<? super t,? super u> other)

bipredicate<integer, integer> bipredicate = (a, b) -> a != b;
bipredicate = bipredicate.or((a, b) -> a == b);
system.out.println(bipredicate.test(1, 1)); // true

④negate()

bipredicate<integer, integer> bipredicate = (a, b) -> a != b;
bipredicate = bipredicate.negate();
system.out.println(bipredicate.test(1, 2)); // false

4.3doublepredicate

接口方法方法描述
boolean test(double value)根据给定的参数进行判断
doublepredicate and(doublepredicate other)返回一个组合判断,将other以短路与的方式加入到函数的判断中
doublepredicate or(doublepredicate other)返回一个组合判断,将other以短路或的方式加入到函数的判断中
default doublepredicate negate()将函数的判断取反

①test(double value)

doublepredicate doublepredicate = doub -> doub != 0;
system.out.println(doublepredicate.test(10)); // true

②and(doublepredicate other)

doublepredicate doublepredicate = doub -> doub != 0;
doublepredicate = doublepredicate.and(doub -> doub < 2);
system.out.println(doublepredicate.test(1.7)); // true

③or(doublepredicate other)

doublepredicate doublepredicate = doub -> doub != 0;
doublepredicate = doublepredicate.or(doub -> doub > 2);
system.out.println(doublepredicate.test(1.7)); // true

④negate()

doublepredicate doublepredicate = doub -> doub != 0;
doublepredicate = doublepredicate.negate();
system.out.println(doublepredicate.test(1.7)); // false

5.supplier供应商

序号接口描述
1supplier<t>不提供输入参数,但是返回结果的函数
2booleansupplier不提供输入参数,但是返回boolean结果的函数
3doublesupplier不提供输入参数,但是返回double结果的函数
4intsupplier不提供输入参数,但是返回int结果的函数
5longsupplier不提供输入参数,但是返回long结果的函数

5.1supplier<t>

接口方法方法描述
t get()获取结果值

①get()

supplier<string> supplier = () ->"hello jack!";
system.out.println(supplier.get()); // hello jack!

5.2booleansupplier

接口方法方法描述
boolean getasboolean()获取函数的执行结果

①getasboolean()

booleansupplier booleansupplier = () -> true;
system.out.println(booleansupplier.getasboolean()); // true

5.3doublesupplier

接口方法方法描述
double getasdouble()获取函数的执行结果

①getasdouble()

doublesupplier doublesupplier = () -> 2.7;
system.out.println(doublesupplier.getasdouble()); // 2.7

6.operator操作员

除了function,consumer,predicate,supplier这几个基本的函数形式,还有其它派生的函数形式,它们扩展了基本的函数形式,包括unaryoperator (extends function)和binaryoperator (extends bifunction)。

序号接口描述
1unaryoperator<t>提供单个类型参数,并且返回一个与输入参数类型一致的结果
2binaryoperator<t>提供两个相同类型参数,并且返回结果与输入参数类型一致的结果
3doublebinaryoperator提供两个double参数并且返回double结果
4doubleunaryoperator提供单个double参数并且返回double结果
5intbinaryoperator提供两个int参数并且返回int结果
6intunaryoperator提供单个int参数并且返回int结果
7longbinaryoperator提供两个long参数并且返回long结果
8longunaryoperator提供单个long参数并且返回long结果

6.1unaryoperator<t>

接口方法方法描述
t apply(t t)将给定参数应用到函数中
function<t, r> andthen(function<? super r,? extends v> after)返回一个组合函数,该函数结果应用到after函数中
function<t, r> compose(function<? super v,? extends t> before)返回一个组合函数,首先将入参应用到before函数,再将before函数结果应用到该函数中

①apply(t t)

unaryoperator<string> unaryoperator = greet -> greet +" bob!";
system.out.println(unaryoperator.apply("hello")); // hello bob!

②andthen(function<? super t,? extends t> after)

unaryoperator<string> unaryoperator = greet -> greet +" bob!";
unaryoperator<string> unaryoperator1 = greet -> greet + " jack!";
string greet = unaryoperator.andthen(unaryoperator1).apply("hello");
system.out.println(greet); // hello bob! jack!

③compose(function<? super t,? extends t> before)

unaryoperator<string> unaryoperator = greet -> greet +" bob!";
unaryoperator<string> unaryoperator1 = greet -> greet + " jack!";
string greet = unaryoperator.compose(unaryoperator1).apply("hello");
system.out.println(greet); // hello jack! bob!

6.2binaryoperator<t>

接口方法方法描述
t apply(t t, t u)根据给定参数执行函数
bifunction<t,t,t> andthen(function<? super t,? extends t> after)返回一个组合函数,after应用于该函数之后
binaryoperator maxby(comparator<? super t> comparator)返回二元操作本身,通过特殊比较器返回最大的元素
binaryoperator minby(comparator<? super t> comparator)返回二元操作本身,通过特殊比较器返回最小的元素

①apply(t t, t u)

binaryoperator<string> binaryoperator = (flag, flag1) -> flag + flag1;
system.out.println(binaryoperator.apply("hello ", "jack!")); // hello jack!

②andthen(function<? super t,? extends t> after)

binaryoperator<string> binaryoperator = (flag, flag1) -> flag + flag1;
function<string, string> function = a -> a +"!!!";
system.out.println(binaryoperator.andthen(function).apply("hello", " jack")); // hello jack!!!

③maxby(comparator<? super t> comparator)

binaryoperator<integer> integerbinaryoperator = binaryoperator.maxby(integer::compareto);integer max = integerbinaryoperator.apply(12, 10);
system.out.println(max); // 12

④minby(comparator<? super t> comparator)

binaryoperator<integer> integerbinaryoperator1 = binaryoperator.minby(integer::compare);integer min = integerbinaryoperator1.apply(12, 10);
system.out.println(min); // 10

6.3doublebinaryoperator

接口方法方法描述
double applyasdouble(double left, double right)根据给定的参数执行函数

①applyasdouble(double left, double right)

doublebinaryoperator doublebinaryoperator = (doub1, doub2) -> doub1
	+ doub2;
system.out.println(doublebinaryoperator.applyasdouble(1.1, 2.3)); // 3.4

6.4doubleunaryoperator

接口方法方法描述
double applyasdouble(double operand)根据给定参数执行函数
doubleunaryoperator andthen(doubleunaryoperator after)返回一个组合函数,after应用于该函数之后
doubleunaryoperator compose(doubleunaryoperator before)返回一个组合函数,before应用于该函数之前

①applyasdouble(double operand)

doubleunaryoperator doubleunaryoperator = doub -> doub + 2.5;
system.out.println(doubleunaryoperator.applyasdouble(2.6)); // 5.1

②andthen(doubleunaryoperator after)

doubleunaryoperator doubleunaryoperator = doub -> doub + 2.5;
doubleunaryoperator doubleunaryoperator1 = doub -> doub * 3;
double result = doubleunaryoperator.andthen(doubleunaryoperator1)
	.applyasdouble(10); 
system.out.println(result); // (10 + 2.5) * 3 = 37.5

③compose(doubleunaryoperator before)

doubleunaryoperator doubleunaryoperator = doub -> doub + 2.5;
doubleunaryoperator doubleunaryoperator1 = doub -> doub * 3;
double result = doubleunaryoperator.compose(doubleunaryoperator1)
	.applyasdouble(10);
system.out.println(result); // 10 * 3 + 2.5 = 32.5

1.函数式接口

函数式接口(functional interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。

functional interface(功能接口)为lambda表达式和方法引用(用冒号::来进行方法的调用)提供目标类型。每个功能接口都有一个抽象方法,称为该功能接口的功能方法,lambda表达式的参数和返回类型与之匹配或适配。功能接口可以在多个上下文中提供目标类型,例如赋值上下文,方法调用或强制转换上下文:

// assignment context
predicate<string> p = string::isempty;
// method invocation context
stream.filter(e -> e.getsize() > 10)...
// cast context
stream.map((tointfunction) e -> e.getsize())...

函数式接口可以使用lambda表达式,方法引用或构造函数引用创建功能接口的实例。

java8为函数式接口引入了一个新注解@functionalinterface,主要用于编译级错误检查,加上该注解,当接口不符合函数式接口定义的时候,编译器会报错。

此注解不是编译器将接口识别为功能接口的必要条件,而仅是帮助捕获设计意图并获得编译器帮助识别意外违反设计意图的帮助。

正确例子,没有报错:

@functionalinterface
public interface helloworldservice {
    void sayhello(string msg);
}

错误例子,接口中包含了两个抽象方法,违反了函数式接口的定义,提示在接口中找到多个非重写的抽象方法。

注意: 加不加 @functionalinterface对于接口是不是函数式接口没有影响,该注解只是提醒编译器去检查该接口是否仅包含一个抽象方法。

1.1允许定义默认方法

函数式接口里是可以包含默认方法,因为默认方法不是抽象方法,其有一个默认实现,所以是符合函数式接口的定义的。

如下代码不会报错:

@functionalinterface
public interface helloworldservice {
    void sayhello(string msg);
    default void dosomework1() {
        // method body
    }
    default void dosomework2() {
        // method body
    }
}

1.2允许定义静态方法

函数式接口里是可以包含静态方法,因为静态方法不能是抽象方法,是一个已经实现了的方法,所以是符合函数式接口的定义的。

如下代码不会报错:

@functionalinterface
public interface helloworldservice {
    void sayhello(string msg);
    static void printhello() {
        system.out.println("hello");
    }
}

1.3允许定义java.lang.object的public方法

函数式接口里是可以包含object里的public方法,这些方法对于函数式接口来说,不被当成是抽象方法(虽然它们是抽象方法);因为任何一个函数式接口的实现,默认都继承了object类,包含了来自java.lang.object里对这些抽象方法的实现;

如下代码不会报错:

@functionalinterface
public interface helloworldservice {
    void sayhello(string msg);
	@override
    boolean equals(object obj);
}

1.4已有函数式接口

函数式接口可以对现有的函数友好地支持lambda。

jdk1.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.changelistener

jdk1.8新增加的函数接口:

  • java.util.function

2.function函数

序号接口描述
1function<t,r>接收一个参数并返回结果的函数
2bifunction<t,u,r>接受两个参数并返回结果的函数
3doublefunction<r>接收一个double类型的参数并返回结果的函数
4doubletointfunction接收一个double类型的参数并返回int结果的函数
5doubletolongfunction接收一个double类型的参数并返回long结果的函数
6intfunction<r>接收一个int类型的参数并返回结果的函数
7inttodoublefunction接收一个int类型的参数并返回double结果的函数
8inttolongfunction接收一个int类型的参数并返回long结果的函数
9longfunction<r>接收一个long类型的参数并返回结果的函数
10longtodoublefunction接收一个long类型的参数并返回double结果的函数
11longtointfunction接收一个long类型的参数并返回int结果的函数
12todoublebifunction<t,u>接收两个参数并返回double结果的函数
13todoublefunction<t>接收一个参数并返回double结果的函数
14tointbifunction<t,u>接收两个参数并返回int结果的函数
15tointfunction<t>接收一个参数并返回int结果的函数
16tolongbifunction<t,u>接收两个参数并返回long结果的函数
17tolongfunction<t>接收一个参数并返回long结果的函数

2.1function<t, r>

接口方法方法描述
r apply(t t)将此参数应用到函数中
function<t, r> andthen(function<? super r,? extends v> after)返回一个组合函数,该函数结果应用到after函数中
function<t, r> compose(function<? super v,? extends t> before)返回一个组合函数,首先将入参应用到before函数,再将before函数结果应用到该函数中

①apply(t t)

function<string, string> function = a -> a +" jack!";
system.out.println(function.apply("hello")); // hello jack!

②andthen(function<? super r,? extends v> after)

function<string, string> function = a -> a +" jack!";
function<string, string> function1 = a -> a + " bob!";
string greet = function.andthen(function1).apply("hello");
system.out.println(greet); // hello jack! bob!

③compose(function<? super v,? extends t> before)

function<string, string> function = a -> a +" jack!";
function<string, string> function1 = a -> a + " bob!";
string greet = function.compose(function1).apply("hello");
system.out.println(greet); // hello bob! jack!

2.2bifunction<t, u, r>

接口方法方法描述
r apply(t t, u u)将参数应用于函数执行
bifunction<t,u,v> andthen(function<? super r,? extends v> after)返回一个组合函数,after函数应用于该函数之后

①apply(t t, u u)

bifunction<string, string, string> bifunction = (a, b) -> a + b;
system.out.println(bifunction.apply("hello ", "jack!")); // hello jack!

②andthen(function<? super r,? extends v> after)

bifunction<string, string, string> bifunction = (a, b) -> a + b;
function<string, string> function = (a) -> a +"!!!";
system.out.println(bifunction.andthen(function).apply("hello", " jack")); // hello jack!!!

2.3doublefunction

接口方法方法描述
r apply(double value)根据给定参数执行函数

①apply(double value)

doublefunction<string> doublefunction = doub ->"结果:" + doub;
system.out.println(doublefunction.apply(1.6)); // 结果:1.6

2.4doubletointfunction

接口方法方法描述
int applyasint(double value)根据给定的参数执行函数

①applyasint(double value)

doubletointfunction doubletointfunction = doub -> double.valueof(doub).intvalue();
system.out.println(doubletointfunction.applyasint(1.2)); // 1

2.5todoublebifunction<t,u>

接口方法方法描述
double applyasdouble(t t, u u)根据给定的参数执行函数

①applyasdouble(t t, u u)

todoublebifunction<long, float> todoublebifunction = (lon, floa) -> lon
	.doublevalue() + floa.doublevalue();
system.out.println(todoublebifunction.applyasdouble(11l, 235.5f)); // 246.5

2.6todoublefunction

接口方法方法描述
double applyasdouble(t value)根据给定参数执行函数

①applyasdouble(t value)

todoublefunction<float> todoublefunction = floa -> floa.doublevalue();
system.out.println(todoublefunction.applyasdouble(12315f)); // 12315.0

3.consumer消费者

序号接口描述
1consumer<t>提供一个t类型的输入参数,不返回执行结果
2biconsumer<t,u>提供两个自定义类型的输入参数,不返回执行结果
3doubleconsumer表示接受单个double值参数,但不返回结果的操作
4intconsumer表示接受单个int值参数,但不返回结果的操作
5longconsumer表示接受单个long值参数,但不返回结果的操作
6objdoubleconsumer<t>表示接受object值和double值,但是不返回任何操作结果
7objintconsumer<t>表示接受object值和int值,但是不返回任何操作结果
8objlongconsumer<t>表示接受object值和long值,但是不返回任何操作结果

3.1consumer<t>

接口方法方法描述
void accept(t t)对给定的参数执行操作
default consumer andthen(consumer<? super t> after)返回一个组合函数,after将会在该函数执行之后应用

①accept(t t)

stringbuilder sb = new stringbuilder("hello ");
consumer<stringbuilder> consumer = (str) -> str.append("jack!");
consumer.accept(sb);
system.out.println(sb.tostring());	// hello jack!

②andthen(consumer<? super t> after)

stringbuilder sb = new stringbuilder("hello ");
consumer<stringbuilder> consumer = (str) -> str.append("jack!");
consumer<stringbuilder> consumer1 = (str) -> str.append(" bob!");
consumer.andthen(consumer1).accept(sb);
system.out.println(sb.tostring());	// hello jack! bob!

3.2biconsumer<t,u>

接口方法方法描述
void accept(t t, u u)对给定的参数执行操作
default biconsumer<t,u> andthen(biconsumer<? super t,? super u> after)返回一个组合函数,after将会在该函数执行之后应用

①accept(t t, u u)

stringbuilder sb = new stringbuilder();
biconsumer<string, string> biconsumer = (a, b) -> {
	sb.append(a);
	sb.append(b);
};
biconsumer.accept("hello ", "jack!");
system.out.println(sb.tostring());	// hello jack!

②andthen(biconsumer<? super t,? super u> after)

stringbuilder sb = new stringbuilder();
biconsumer<string, string> biconsumer = (a, b) -> {
	sb.append(a);
	sb.append(b);
};
biconsumer<string, string> biconsumer1 = (a, b) -> {
	system.out.println(a + b);
};
biconsumer.andthen(biconsumer1).accept("hello", " jack!"); // hello jack!

3.3doubleconsumer

接口方法方法描述
void accept(double value)对给定的参数执行操作
default doubleconsumer andthen(doubleconsumer after)返回一个组合函数,after在该函数执行之后应用

①accept(double value)

doubleconsumer doubleconsumer = system.out::println;
doubleconsumer.accept(1.3); // 1.3

②andthen(doubleconsumer after)

doubleconsumer doubleconsumer = system.out::println;
doubleconsumer doubleconsumer1 = system.out::println;
doubleconsumer.andthen(doubleconsumer1).accept(1.3);
// 1.3  
// 1.3

3.4objdoubleconsumer<t>

接口方法方法描述
void accept(t t, double value)对给定的参数执行操作

①accept(t t, double value)

objdoubleconsumer<string> doubleconsumer = (obj, doub)
	-> system.out.println(obj + doub);
doubleconsumer.accept("金额:", 222.66); // 金额:222.66

4.predicate谓语

序号接口描述
1predicate<t>对给定的输入参数执行操作,返回一个boolean类型的结果(布尔值函数)
2bipredicate<t,u>对给定的两个输入参数执行操作,返回一个boolean类型的结果(布尔值函数)
3doublepredicate对给定的double参数执行操作,返回一个boolean类型的结果(布尔值函数)
4intpredicate对给定的int输入参数执行操作,返回一个boolean类型的结果(布尔值函数)
5longpredicate对给定的long参数执行操作,返回一个boolean类型的结果(布尔值函数)

4.1predicate<t>

接口方法方法描述
boolean test(t t)根据给定的参数进行判断
predicate and(predicate<? super t> other)返回一个组合判断,将other以短路与的方式加入到函数的判断中
predicate or(predicate<? super t> other)返回一个组合判断,将other以短路或的方式加入到函数的判断中
predicate negate()将函数的判断取反

①test(t t)

predicate<integer> predicate = number -> number !=0;
system.out.println(predicate.test(10));    //true

②and(predicate<? super t> other)

predicate<integer> predicate = number -> number !=0;
predicate = predicate.and(number -> number >= 10);
system.out.println(predicate.test(10));    //true

③or(predicate<? super t> other)

predicate<integer> predicate = number -> number !=0;
predicate = predicate.or(number -> number != 10);
system.out.println(predicate.test(10));    //true

④negate()

predicate<integer> predicate = number -> number !=0;
predicate = predicate.negate();
system.out.println(predicate.test(10));    //false

4.2bipredicate<t,u>

接口方法方法描述
boolean test(t t, u u)根据给定的两个输入参数进行判断
bipredicate<t,u> and(bipredicate<? super t,? super u> other)返回一个组合判断,将other以短路与的方式加入到函数的判断中
bipredicate<t,u> or(bipredicate<? super t,? super u> other)返回一个组合判断,将other以短路或的方式加入到函数的判断中
bipredicate<t,u> negate()将函数的判断取反

①test(t t, u u)

bipredicate<integer, integer> bipredicate = (a, b) -> a != b;
system.out.println(bipredicate.test(1, 2)); // true

②and(bipredicate<? super t,? super u> other)

bipredicate<integer, integer> bipredicate = (a, b) -> a != b;
bipredicate = bipredicate.and((a, b) -> a.equals(b));
system.out.println(bipredicate.test(1, 2)); // false

③or(bipredicate<? super t,? super u> other)

bipredicate<integer, integer> bipredicate = (a, b) -> a != b;
bipredicate = bipredicate.or((a, b) -> a == b);
system.out.println(bipredicate.test(1, 1)); // true

④negate()

bipredicate<integer, integer> bipredicate = (a, b) -> a != b;
bipredicate = bipredicate.negate();
system.out.println(bipredicate.test(1, 2)); // false

4.3doublepredicate

接口方法方法描述
boolean test(double value)根据给定的参数进行判断
doublepredicate and(doublepredicate other)返回一个组合判断,将other以短路与的方式加入到函数的判断中
doublepredicate or(doublepredicate other)返回一个组合判断,将other以短路或的方式加入到函数的判断中
default doublepredicate negate()将函数的判断取反

①test(double value)

doublepredicate doublepredicate = doub -> doub != 0;
system.out.println(doublepredicate.test(10)); // true

②and(doublepredicate other)

doublepredicate doublepredicate = doub -> doub != 0;
doublepredicate = doublepredicate.and(doub -> doub < 2);
system.out.println(doublepredicate.test(1.7)); // true

③or(doublepredicate other)

doublepredicate doublepredicate = doub -> doub != 0;
doublepredicate = doublepredicate.or(doub -> doub > 2);
system.out.println(doublepredicate.test(1.7)); // true

④negate()

doublepredicate doublepredicate = doub -> doub != 0;
doublepredicate = doublepredicate.negate();
system.out.println(doublepredicate.test(1.7)); // false

5.supplier供应商

序号接口描述
1supplier<t>不提供输入参数,但是返回结果的函数
2booleansupplier不提供输入参数,但是返回boolean结果的函数
3doublesupplier不提供输入参数,但是返回double结果的函数
4intsupplier不提供输入参数,但是返回int结果的函数
5longsupplier不提供输入参数,但是返回long结果的函数

5.1supplier<t>

接口方法方法描述
t get()获取结果值

①get()

supplier<string> supplier = () ->"hello jack!";
system.out.println(supplier.get()); // hello jack!

5.2booleansupplier

接口方法方法描述
boolean getasboolean()获取函数的执行结果

①getasboolean()

booleansupplier booleansupplier = () -> true;
system.out.println(booleansupplier.getasboolean()); // true

5.3doublesupplier

接口方法方法描述
double getasdouble()获取函数的执行结果

①getasdouble()

doublesupplier doublesupplier = () -> 2.7;
system.out.println(doublesupplier.getasdouble()); // 2.7

6.operator操作员

除了function,consumer,predicate,supplier这几个基本的函数形式,还有其它派生的函数形式,它们扩展了基本的函数形式,包括unaryoperator (extends function)和binaryoperator (extends bifunction)。

序号接口描述
1unaryoperator<t>提供单个类型参数,并且返回一个与输入参数类型一致的结果
2binaryoperator<t>提供两个相同类型参数,并且返回结果与输入参数类型一致的结果
3doublebinaryoperator提供两个double参数并且返回double结果
4doubleunaryoperator提供单个double参数并且返回double结果
5intbinaryoperator提供两个int参数并且返回int结果
6intunaryoperator提供单个int参数并且返回int结果
7longbinaryoperator提供两个long参数并且返回long结果
8longunaryoperator提供单个long参数并且返回long结果

6.1unaryoperator<t>

接口方法方法描述
t apply(t t)将给定参数应用到函数中
function<t, r> andthen(function<? super r,? extends v> after)返回一个组合函数,该函数结果应用到after函数中
function<t, r> compose(function<? super v,? extends t> before)返回一个组合函数,首先将入参应用到before函数,再将before函数结果应用到该函数中

①apply(t t)

unaryoperator<string> unaryoperator = greet -> greet +" bob!";
system.out.println(unaryoperator.apply("hello")); // hello bob!

②andthen(function<? super t,? extends t> after)

unaryoperator<string> unaryoperator = greet -> greet +" bob!";
unaryoperator<string> unaryoperator1 = greet -> greet + " jack!";
string greet = unaryoperator.andthen(unaryoperator1).apply("hello");
system.out.println(greet); // hello bob! jack!

③compose(function<? super t,? extends t> before)

unaryoperator<string> unaryoperator = greet -> greet +" bob!";
unaryoperator<string> unaryoperator1 = greet -> greet + " jack!";
string greet = unaryoperator.compose(unaryoperator1).apply("hello");
system.out.println(greet); // hello jack! bob!

6.2binaryoperator<t>

接口方法方法描述
t apply(t t, t u)根据给定参数执行函数
bifunction<t,t,t> andthen(function<? super t,? extends t> after)返回一个组合函数,after应用于该函数之后
binaryoperator maxby(comparator<? super t> comparator)返回二元操作本身,通过特殊比较器返回最大的元素
binaryoperator minby(comparator<? super t> comparator)返回二元操作本身,通过特殊比较器返回最小的元素

①apply(t t, t u)

binaryoperator<string> binaryoperator = (flag, flag1) -> flag + flag1;
system.out.println(binaryoperator.apply("hello ", "jack!")); // hello jack!

②andthen(function<? super t,? extends t> after)

binaryoperator<string> binaryoperator = (flag, flag1) -> flag + flag1;
function<string, string> function = a -> a +"!!!";
system.out.println(binaryoperator.andthen(function).apply("hello", " jack")); // hello jack!!!

③maxby(comparator<? super t> comparator)

binaryoperator<integer> integerbinaryoperator = binaryoperator.maxby(integer::compareto);integer max = integerbinaryoperator.apply(12, 10);
system.out.println(max); // 12

④minby(comparator<? super t> comparator)

binaryoperator<integer> integerbinaryoperator1 = binaryoperator.minby(integer::compare);integer min = integerbinaryoperator1.apply(12, 10);
system.out.println(min); // 10

6.3doublebinaryoperator

接口方法方法描述
double applyasdouble(double left, double right)根据给定的参数执行函数

①applyasdouble(double left, double right)

doublebinaryoperator doublebinaryoperator = (doub1, doub2) -> doub1
	+ doub2;
system.out.println(doublebinaryoperator.applyasdouble(1.1, 2.3)); // 3.4

6.4doubleunaryoperator

接口方法方法描述
double applyasdouble(double operand)根据给定参数执行函数
doubleunaryoperator andthen(doubleunaryoperator after)返回一个组合函数,after应用于该函数之后
doubleunaryoperator compose(doubleunaryoperator before)返回一个组合函数,before应用于该函数之前

①applyasdouble(double operand)

doubleunaryoperator doubleunaryoperator = doub -> doub + 2.5;
system.out.println(doubleunaryoperator.applyasdouble(2.6)); // 5.1

②andthen(doubleunaryoperator after)

doubleunaryoperator doubleunaryoperator = doub -> doub + 2.5;
doubleunaryoperator doubleunaryoperator1 = doub -> doub * 3;
double result = doubleunaryoperator.andthen(doubleunaryoperator1)
	.applyasdouble(10); 
system.out.println(result); // (10 + 2.5) * 3 = 37.5

③compose(doubleunaryoperator before)

doubleunaryoperator doubleunaryoperator = doub -> doub + 2.5;
doubleunaryoperator doubleunaryoperator1 = doub -> doub * 3;
double result = doubleunaryoperator.compose(doubleunaryoperator1)
	.applyasdouble(10);
system.out.println(result); // 10 * 3 + 2.5 = 32.5

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com