/*
* BiPredicate<T, U> , 传递两个参数,返回一个boolean值
*/
BiPredicate<Integer, Integer> biPredicate = (x, y) -> x > y;
System.out.println(biPredicate.test(3 ,4));
// BooleanSupplier 代表了 boolean 值结果的提供方
int num = 4;
System.out.println(getMsg(() -> (num % 3 == 0)));
DoublePredicate doublePredicate = x -> (x + 1) > 4;
System.out.println(doublePredicate.test(5));
filterAppleRed(new ArrayList<>(), red -> red.getColor().equals("red"));
/*
* 接受一个参数处理,没有返回值
*/
Consumer<String> consumer = System.out::println;
consumer.accept("ddd");
DoubleConsumer doubleConsumer = System.out::println;
doubleConsumer.accept(3d);
/*
* 接受两个参数处理,没有返回值BiConsumer<T, U>
*/
BiConsumer<Integer, Integer> biConsumer = (x, y) -> System.out.println(x + y);
biConsumer.accept(1, 1);
/*
* 三个参数BiFunction<T, U, R>, 如参数是T, U 返回 R
*/
BiFunction<String, Integer, Apple> biFunction = Apple::new;
System.out.println(biFunction.apply("gao", 111));
/*
* 返回一个double类型
*/
DoubleFunction<Double> doubleFunction = (x) -> { return x * 2;};
System.out.println(doubleFunction.apply(3));
/*
* BinaryOperator 继承 BiFunction,然后三个参数类型一致
*/
BinaryOperator<Integer> binaryOperator = Integer::sum;
System.out.println(binaryOperator.apply(2, 4));
BinaryOperator<Apple> appleBinaryOperator = BinaryOperator.maxBy(Comparator.comparing(Apple::getWeight));
Apple apply = appleBinaryOperator.apply(new Apple("red", 1), new Apple("red", 2));
System.out.println(apply);
/*
* double 输入连个参数返回一个参数,就是固定的dubbo;
*/
DoubleBinaryOperator doubleBinaryOperator = (x, y) -> x + y;
System.out.println(doubleBinaryOperator.applyAsDouble(1d, 2.1d));
/*
* 通过double返回int
*/
DoubleToIntFunction doubleToIntFunction = x -> (int) x + 1;
System.out.println(doubleToIntFunction.applyAsInt(3.1d));
IntFunction<Integer> intFunction = x -> x * 4;
System.out.println(intFunction.apply(3));
/*
* 通过double返回long
*/
DoubleToLongFunction doubleToLongFunction = s -> (long)s + 3;
System.out.println(doubleToLongFunction.applyAsLong(333d));
DoubleUnaryOperator doubleUnaryOperator = d -> d;
System.out.println(doubleUnaryOperator.applyAsDouble(3d));
IntBinaryOperator intBinaryOperator = (x, y) -> x+ y;
intBinaryOperator.applyAsInt(1, 3);
IntPredicate intPredicate = s -> (s + 1) > 4;
System.out.println(intPredicate.test(3));
IntToDoubleFunction intToDoubleFunction = s -> Double.parseDouble(String.valueOf(s));
intToDoubleFunction.applyAsDouble(3);
IntToLongFunction intToLongFunction = s -> (long) s;
intToLongFunction.applyAsLong(4);
IntUnaryOperator intUnaryOperator = s -> s + 1;
intUnaryOperator.applyAsInt(3);
LongBinaryOperator longBinaryOperator = (x, y) -> x + y;
longBinaryOperator.applyAsLong(2L , 4L);
LongConsumer longConsumer = s -> System.out.println(s);
longConsumer.accept(3L);
LongFunction<String> longFunction = String::valueOf;
System.out.println(longFunction.apply(4));
SayFunctionInterface sayFunctionInterface = (x ,y) -> System.out.println(x + y);
sayFunctionInterface.accept("dd", 4);
java8 示例
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- SpringBoot3.0最低支持JDK17,各开源软件正在全面拥抱JDK17. 在2022年Java开发人员生产...
- 转载自:http://www.importnew.com/16436.html Java 8 刚于几周前发布,日期...
- 不出意外的话,再过几天,Java 11就要正式对外发布了,不知各位同行都用上哪个版本了呢?先贴一张截取的调查图,由...
- 1、用Optional封装可能为null的值 现存Java API几乎都是通过返回一个null的方式来表示需要值的...