Java8 常用FunctionInterface使用方法
来源:互联网 发布:开淘宝能赚钱吗 编辑:程序博客网 时间:2024/06/08 10:07
package com.lgx.jdk8.part02;import java.util.*;import java.util.function.*;/** * java.util.function包API自学 */public class Test06LearnBySelf { public static void main(String[] args) { /**Consumer:接收一个参数不返回值,functional method is accept(Object)**/ List<String> list = Arrays.asList("hello", "world"); list.forEach(new Consumer<String>() { @Override public void accept(String s) { System.out.println("s = " + s.toUpperCase()); } }); list.forEach(item -> System.out.println(item.toUpperCase())); /**DoubleConsumer:接收一个Double参数不返回值,functional method is accept(double)**/ DoubleConsumer dc = (x) -> System.out.println(x*x); dc.accept(3.1415); List<Double> list2 = Arrays.asList(1.11, 2.22); /*list2.forEach(new DoubleConsumer(){ @Override public void accept(double value) { } });*/ //一直报错???原因,forEach方法接受的参数就是Consumer,DoubleConsumer不是Consumer的子类 list2.forEach(item -> System.out.println(item)); /**BiConsumer:接收两个参数不返回值,functional method is accept(Object, Object)**/ Map<String, String> map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", "value2"); map.forEach(new BiConsumer<String, String>() { @Override public void accept(String s1, String s2) { System.out.println("key = " + s1 + ", value = " + s2); } }); map.forEach((s1, s2) -> System.out.println("key = " + s1 + ", value = " + s2)); System.out.println("================================================================"); /**Function:接收一个参数返回一个值,functional method is apply(Object)**/ Function<String, String> function = (s1) -> s1.toUpperCase(); String result = function.apply("hello world"); System.out.println("result = " + result); /**IntFunction:接收一个Int参数返回一个Int值,functional method is apply(int)**/ IntFunction<Integer> intFunction = (int1) -> int1 * int1; int intResult = intFunction.apply(100); System.out.println("intResult = " + intResult); /**DoubleToIntFunction:接收一个double参数返回一个int值,functional method is applyAsInt(Object)**/ DoubleToIntFunction doubleToIntFunction = (d1) -> (int)d1+2; int doubleToIntResult = doubleToIntFunction.applyAsInt(3.1415926); System.out.println("doubleToIntResult = " + doubleToIntResult); /**BiFunction:接收两个参数返回一个值,functional method is apply(Object, Object)**/ BiFunction<String, String, String> biFunction = (s1, s2) -> s1 + s2; String biResult = biFunction.apply("hello", "world"); System.out.println("biResult = " + biResult); /**BinaryOperator(二元运算符):接收两个参数返回一个值(参数和返回值类型是一致的),functional method is apply(Object, Object)**/ BinaryOperator<Double> binaryOperator = (d1, d2) -> d1 + d2; double binaryResult = binaryOperator.apply(20.0, 30.0); System.out.println("binaryResult = " + binaryResult); double minResult = BinaryOperator.minBy(Double::compareTo).apply(20.0 , 30.0); System.out.println("minResult = " + minResult); double maxResult = BinaryOperator.maxBy(Double::compareTo).apply(20.0 , 30.0); System.out.println("maxResult = " + maxResult); System.out.println("================================================================"); /**Predicate:接收一个参数返回一个boolean值,functional method is test(Object)**/ Predicate<String> predicate = (a) -> a == null; boolean predicateResult = predicate.test(new String()); System.out.println("predicateResult = " + predicateResult); /**DoublePredicate:接收一个参数返回一个boolean值,functional method is test(double)**/ DoublePredicate doublePredicate = (d) -> d >= 10; boolean doublePredicateResult = doublePredicate.test(9.99999); System.out.println("doublePredicateResult = " + doublePredicateResult); /**BiPredicate:接收两个参数返回一个boolean值,functional method is test(Object, Object)**/ BiPredicate<Integer, Integer> biPredicate = (b1, b2) -> b1 > b2; boolean biPredicateResult = biPredicate.test(3, 2); System.out.println("biPredicateResult = " + biPredicateResult); System.out.println("================================================================"); /**Supplier:不接受参数返回一个值,functional method is get(Object)**/ Supplier<String> supplier = () -> "test"; String supplierResult = supplier.get(); System.out.println("supplierResult = " + supplierResult); /**DoubleSupplier:不接受参数返回一个Double值,functional method is getAsDouble()**/ DoubleSupplier doubleSupplier = () -> 1; double doubleSupplierResult = doubleSupplier.getAsDouble(); System.out.println("doubleSupplierResult = " + doubleSupplierResult); System.out.println("================================================================"); }}
阅读全文
0 0
- Java8 常用FunctionInterface使用方法
- Java8 FunctionInterface
- Java8 Stream常用方法
- JAVA8自定义Lambda表达式的常见使用方法
- java8 常用操作再续
- Java8-常用的流操作
- Java8 lambda表达式常用方法
- java8 stream 常用的方法
- Java8 LocalDate/LocalDateTime常用方法
- java8 lambda表达式之 Stream常用方法
- 常用Java8新特性简单介绍
- Java8 常用的Stream流操作
- 常用list,map操作之java8
- Java8
- java8
- Java8
- java8
- java8
- xml工具箱phpcms
- jquery内容过滤选择器:内容过滤选择器它是根据元素内部文本内容进行选中。
- TF-slim 调用slim提供的网络模型训练自己的数据
- 阶段总结——软件工程视频(二)
- Java compiler level does not match the version of the installed Java project facet.
- Java8 常用FunctionInterface使用方法
- ROS 学习系列 -- Roomba, Xtion Pro机器人制作地图在Android手机无法实时观测地图
- 51nod 1779 逆序对统计【状压DP】
- hadoop踩坑记2--伪分布式部署
- Spectrum数字化仪为东京大学最高的室内磁场中心提供核心部件
- MACOS下解决git push error: Permission to XXX.git denied to XXX
- HDU5800 To My Girlfrend(计数dp)
- 前端请求跨域理解
- 静态导入 import static