java 方法描述符_Java8常用的函数式接口以及函数描述符

java8共提供了43个函数式接口,根据函数描述符可分为下面9种情况,其余均是为消除装箱拆箱所带来的性能影响而对原始类型特化产生的。

Predicate < T >

函数描述符: T -> boolean

原始类型特化:IntPredicate, LongPredicate, DoublePredicate

@Test

void predicate() {

IntPredicate predicate = i -> (i > 0);

// true

System.out.println(predicate.test(5));

}

Consumer < T >

函数描述符: T -> void

原始类型特化:IntConsumer,LongConsumer, DoubleConsumer

@Test

void consumer() {

// 输出数字到控制台

IntConsumer consumer = i -> System.out.println(i);

//IntConsumer consumer = System.out::println;

consumer.accept(5);

}

Function < T , R >

函数描述符: T -> R

原始类型特化:

IntFunction < R >, IntToDoubleFunction, IntToLongFunction,

LongFunction < R >, LongToDoubleFunction, LongToIntFunction,

DoubleFunction < R >,

ToIntFunction < T >, ToDoubleFunction < T >, ToLongFunction < T >

@Test

void function() {

// 整型转字符串

IntFunction int2StringFunc = i -> (i + "");

//"10"

int2StringFunc.apply(10);

// 字符串转整型

ToIntFunction string2IntFunc = s -> Integer.parseInt(s);

//ToIntFunction string2IntFunc = Integer::parseInt;

// 22

string2IntFunc.applyAsInt("22");

}

Supplier < T >

函数描述符: () -> T

原始类型特化:BooleanSupplier,IntSupplier, LongSupplier, DoubleSupplier

@Test

void supplier() {

// 无中生有

Supplier supplier = () -> "Welcome to my blog";

// "Welcome to my blog"

supplier.get();

Supplier> listSupplier = ArrayList::new;

listSupplier.get().add("hello, world");

}

UnaryOperator < T >

函数描述符: T -> T

原始类型特化:IntUnaryOperator, LongUnaryOperator, DoubleUnaryOperator

@Test

void unaryOperator() {

// 平方

IntUnaryOperator operator = i -> i * i;

// 25

operator.applyAsInt(5);

}

BinaryOperator< T >

函数描述符: (T, T) -> T

原始类型特化:IntBinaryOperator, LongBinaryOperator, DoubleBinaryOperator

@Test

void binaryOperator() {

// 两数之和

IntBinaryOperator sum = (i, j) -> i + j;

// IntBinaryOperator sum = Integer::sum;

// 11

sum.applyAsInt(5, 6);

}

BiPredicate < L, R >

函数描述符: (L, R) -> boolean

原始类型特化:无

@Test

void biPredicate() {

// 判断数值是否相等

BiPredicate biPredicate = (i, s) -> i == Integer.parseInt(s);

// true

biPredicate.test(5, "5");

// false

biPredicate.test(5, "4");

}

BiConsumer < T, U >

函数描述符: (T, U) -> void

原始类型特化:ObjIntConsumer < T >, ObjLongConsumer < T >, ObjDoubleConsumer < T >

@Test

void biConsumer() {

String format = "%s is %d years old";

BiConsumer biConsumer = (s, i) -> {

String result = String.format(format, s, i);

System.out.println(result);

};

// Tom is 15 years old

biConsumer.accept("Tom", 15);

}

BiFunction < T, U, R >

函数描述符: (T, U) -> R

原始类型特化:ToIntBiFunction < T, U >, ToDoubleBiFunction < T, U >, ToLongBiFunction < T, U >

@Test

void biFunction() {

// 加入HashMap中

BiFunction biFunction = (s, i) -> {

Map map = new HashMap<>();

map.put(s, i);

return map;

};

// {"key" : 100}

biFunction.apply("key", 100);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值