- jdk9都出来了,我还在看jdk8
- 流式数据处理
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; /** * Created by micocube * ProjectName: spring-web * PackageName: com.mico.jdk8 * User: micocube * CreateTime: 2018/4/21下午12:37 * ModifyTime: 2018/4/21下午12:37 * Version: 0.1 * Description:jdk8 流式数据处理相关例子 **/ public class StreamApi { public static void main(String[] args) { //初始化 List
integerList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1); //过滤,收集所有偶数 List collect = integerList.stream() .filter(integer -> integer % 2 == 0) .collect(Collectors.toList()); System.out.println("过滤,收集所有偶数:" + collect); //计算总和 int sum = integerList.stream() .mapToInt(Integer::intValue) .sum(); System.out.println("计算总和:" + sum); //计算总和 int reduce = integerList .stream() .mapToInt(Integer::intValue) .reduce(0, Integer::sum);//带初始值 System.out.println("计算总和:" + reduce); OptionalInt reduce1 = integerList .stream() .mapToInt(Integer::intValue) .reduce(Integer::sum);//不带初始值 System.out.println("计算总和:" + reduce1); Integer reduce2 = integerList .stream() .reduce(0, (a, b) -> a + b); System.out.println("计算总和:" + reduce2); //计算数量 long count = integerList.stream().count(); System.out.println("计算数量" + count); Optional collect8 = integerList.stream().collect(Collectors.maxBy((x1, x2) -> x1 - x2)); Optional collect9 = integerList.stream().collect(Collectors.maxBy(Comparator.comparing(Integer::intValue))); if (collect8.isPresent()) System.out.println("求最大值:" + collect8.get()); if (collect9.isPresent()) System.out.println("求最大值:" + collect9.get()); Optional collect10 = integerList.stream().collect(Collectors.minBy(Comparator.comparing(Integer::intValue))); if (collect10.isPresent()) System.out.println("求最小值:" + collect10.get()); //求平均值 Double collect11 = integerList.stream().collect(Collectors.averagingInt(Integer::intValue)); System.out.println("求平均值:" + collect11); //一次性得到元素个数、总和、均值、最大值、最小值 IntSummaryStatistics collect12 = integerList.stream().collect(Collectors.summarizingInt(Integer::intValue)); System.out.println("一次性得到元素个数、总和、均值、最大值、最小值:" + collect12); //分组 Map > collect15 = integerList.stream().collect( Collectors.groupingBy(Integer::intValue) ); System.out.println("分组:" + collect15); Map collect14 = integerList.stream().collect( Collectors.groupingBy(Integer::intValue, Collectors.counting()) ); System.out.println("可以有多级分组:" + collect14); //分区可以看做是分组的一种特殊情况,在分区中key只有两种情况:true或false Map > collect16 = integerList.stream().collect(Collectors.partitioningBy(x -> x >= 7)); System.out.println("分区可以看做是分组的一种特殊情况,在分区中key只有两种情况:true或false:" + collect16); //去重 List collect1 = integerList .stream() .distinct() .collect(Collectors.toList()); System.out.println("去重:" + collect1); //limit返回包含前n个元素的流 List collect2 = integerList .stream() .filter(integer -> integer % 2 == 0).limit(2) .collect(Collectors.toList()); System.out.println("limit:" + collect2); //排序,倒序排序 List collect3 = integerList. stream() .sorted((s1, s2) -> s2 - s1) .collect(Collectors.toList()); System.out.println("排序:" + collect3); //跳过前n个元素 List collect4 = integerList .stream() .filter(integer -> integer % 2 == 1).skip(2) .collect(Collectors.toList()); System.out.println("skip:" + collect4); String[] strs = {"java8", "is", "easy", "to", "use"}; //字符串拼接 String collect13 = Arrays.stream(strs).collect(Collectors.joining()); System.out.println("字符串拼接:" + collect13); List collect5 = Arrays.stream(strs) .map(s -> s.split(""))//将字符串映射成字符数组 .collect(Collectors.toList()); System.out.println("将字符串映射成字符数组:" + collect5); //flatMap是将一个流中的每个值都转成一个个流,然后再将这些流扁平化成为一个流 List collect7 = Arrays.stream(strs) .map(s -> s.split(""))//每个字符串映射成string[] .flatMap(Arrays::stream)//flatMap将由map映射得到的Stream ,转换成由各个字符串数组映射成的流Stream .collect(Collectors.toList()); System.out.println("flatMap是将一个流中的每个值都转成一个个流,然后再将这些流扁平化成为一个流:" + collect7); //多个字符串将各个字符拆开,去重 List collect6 = Arrays.stream(strs) .map(s -> s.split("")) .flatMap(Arrays::stream) .distinct() .collect(Collectors.toList()); System.out.println("多个字符串将各个字符拆开,去重:" + collect6); //allMatch,检测是否全部都满足指定的参数行为 boolean b = integerList.stream().allMatch(integer -> integer > 5); System.out.println("allMatch,检测是否全部都满足指定的参数行为:" + b); //anyMatch,检测是否存在一个或多个满足指定的参数行为 boolean any = integerList.stream().anyMatch(integer -> integer > 5); System.out.println("anyMatch,检测是否存在一个或多个满足指定的参数行为:" + any); //nonMatch 检测是否不存在满足指定行为的元素 boolean non = integerList.stream().noneMatch(integer -> integer > 5); System.out.println("nonMatch 检测是否不存在满足指定行为的元素:" + non); //用于返回满足条件的第一个元素 Optional first = integerList.stream().filter(integer -> integer > 6).findFirst(); if (first.isPresent()) System.out.println("用于返回满足条件的第一个元素:" + first.get()); //findAny相对于findFirst的区别在于,findAny不一定返回第一个,而是返回任意一个 //实际上对于顺序流式处理而言,findFirst和findAny返回的结果是一样的, // 至于为什么会这样设计,当我们启用并行流式处理的时候,查找第一个元素往往会有很多限制,如果不是特别需求, // 在并行流式处理中使用findAny的性能要比findFirst好。 Optional any1 = integerList.stream().filter(integer -> integer > 1).distinct().findAny(); if (first.isPresent()) System.out.println("findAny不一定返回第一个,而是返回任意一个:" + any1.get()); // 启动并行流式处理虽然简单,只需要将stream()替换成parallelStream()即可, // 但既然是并行,就会涉及到多线程安全问题,所以在启用之前要先确认并行是否值得 // (并行的效率不一定高于顺序执行),另外就是要保证线程安全。此两项无法保证, // 那么并行毫无意义,毕竟结果比速度更加重 Optional any2 = integerList.parallelStream().filter(integer -> integer > 1).distinct().findAny(); if(any2.isPresent())System.out.println("并行流式处理:"+any2.get()); } }
四大核心函数式接口Function、Consumer、Supplier、Predicate
Function
T:入参类型,R:出参类型
调用方法:R apply(T t);
定义函数示例:Function
func = p -> p * 10; // 输出入参的10倍 调用函数示例:func.apply(10); // 结果100
Consumer
T:入参类型;没有出参
调用方法:void accept(T t);
定义函数示例:Consumer
consumer= p -> System.out.println(p); // 因为没有出参,常用于打印、发送短信等消费动作 调用函数示例:consumer.accept("18800008888");
Supplier
T:出参类型;没有入参
调用方法:T get();
定义函数示例:Supplier
supplier= () -> 100; // 常用于业务“有条件运行”时,符合条件再调用获取结果的应用场景;运行结果须提前定义,但不运行。 调用函数示例:supplier.get();
Predicate
T:入参类型;出参类型是Boolean
调用方法:boolean test(T t);
定义函数示例:Predicate
predicate = p -> p % 2 == 0; // 判断是否、是不是偶数 调用函数示例:predicate.test(100); // 运行结果true
Map(发散)和Reduce(聚合)例子专场
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.function.*;
import java.util.stream.Stream;
/**
* @author micocube
* projectName: web_admin
* packageName: com.micocube.web.admin
* email: [email protected]
* createTime: 2019-09-25 14:16
* version: 0.1
* description:
*/
public class Lambda {
@Test
public void test(){
/**
*
* Consumer消费型接口,接收数据并处理
* void accept(T t);
*/
Consumer println = System.out::println;
/**
* 与上面的意义相同
* Consumer println2 = param -> System.out.println(param);
* 结果:
* abc
*/
println.accept("abc");
/**
*Supplier: 供给型接口,对外提供数据
* T get()
*/
Supplier getProperties = System::getProperties;
Properties properties = getProperties.get();
/**
* 结果:
* {java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib, java.vm.version=25.131-b11, gopherProxySet=false, java.vm.vendor=Oracle Corporation, java.vendor.url=http://java.oracle.com/, path.separator=:, java.vm.name=Java HotSpot(TM) 64-Bit Server VM, file.encoding.pkg=sun.io, user.country=CN, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=unknown, java.vm.specification.name=Java Virtual Machine Specification, user.dir=/Users/micocube/Desktop/java/web_admin, java.runtime.version=1.8.0_131-b11, java.awt.graphicsenv=sun.awt.CGraphicsEnvironment, java.endorsed.dirs=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/endorsed, os.arch=x86_64, java.io.tmpdir=/var/folders/2f/6g9vp18j7t15np9rtwvcwxb40000gn/T/, line.separator=
* , java.vm.specification.vendor=Oracle Corporation, os.name=Mac OS X, sun.jnu.encoding=UTF-8, java.library.path=/Users/micocube/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:., java.specification.name=Java Platform API Specification, java.class.version=52.0, sun.management.compiler=HotSpot 64-Bit Tiered Compilers, os.version=10.13.1, user.home=/Users/micocube, user.timezone=, java.awt.printerjob=sun.lwawt.macosx.CPrinterJob, file.encoding=UTF-8, java.specification.version=1.8, java.class.path=/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit-rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit5-rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/tools.jar:/Users/micocube/Desktop/java/web_admin/target/test-classes:/Users/micocube/Desktop/java/web_admin/target/classes:/soft/apache-maven-3.5.0/repository/org/mybatis/spring/boot/mybatis-spring-boot-starter/2.1.0/mybatis-spring-boot-starter-2.1.0.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-starter/2.1.7.RELEASE/spring-boot-starter-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-starter-logging/2.1.7.RELEASE/spring-boot-starter-logging-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/soft/apache-maven-3.5.0/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/soft/apache-maven-3.5.0/repository/org/apache/logging/log4j/log4j-to-slf4j/2.11.2/log4j-to-slf4j-2.11.2.jar:/soft/apache-maven-3.5.0/repository/org/apache/logging/log4j/log4j-api/2.11.2/log4j-api-2.11.2.jar:/soft/apache-maven-3.5.0/repository/org/slf4j/jul-to-slf4j/1.7.26/jul-to-slf4j-1.7.26.jar:/soft/apache-maven-3.5.0/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar:/soft/apache-maven-3.5.0/repository/org/yaml/snakeyaml/1.23/snakeyaml-1.23.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-starter-jdbc/2.1.7.RELEASE/spring-boot-starter-jdbc-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/com/zaxxer/HikariCP/3.2.0/HikariCP-3.2.0.jar:/soft/apache-maven-3.5.0/repository/org/springframework/spring-jdbc/5.1.9.RELEASE/spring-jdbc-5.1.9.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/mybatis/spring/boot/mybatis-spring-boot-autoconfigure/2.1.0/mybatis-spring-boot-autoconfigure-2.1.0.jar:/soft/apache-maven-3.5.0/repository/org/mybatis/mybatis/3.5.2/mybatis-3.5.2.jar:/soft/apache-maven-3.5.0/repository/org/mybatis/mybatis-spring/2.0.2/mybatis-spring-2.0.2.jar:/soft/apache-maven-3.5.0/repository/com/h2database/h2/1.4.199/h2-1.4.199.jar:/soft/apache-maven-3.5.0/repository/org/projectlombok/lombok/1.18.8/lombok-1.18.8.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-starter-security/2.1.7.RELEASE/spring-boot-starter-security-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/spring-aop/5.1.9.RELEASE/spring-aop-5.1.9.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/spring-beans/5.1.9.RELEASE/spring-beans-5.1.9.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/security/spring-security-config/5.1.6.RELEASE/spring-security-config-5.1.6.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/spring-context/5.1.9.RELEASE/spring-context-5.1.9.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/security/spring-security-web/5.1.6.RELEASE/spring-security-web-5.1.6.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/spring-expression/5.1.9.RELEASE/spring-expression-5.1.9.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-starter-data-jpa/2.1.7.RELEASE/spring-boot-starter-data-jpa-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-starter-aop/2.1.7.RELEASE/spring-boot-starter-aop-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/aspectj/aspectjweaver/1.9.4/aspectjweaver-1.9.4.jar:/soft/apache-maven-3.5.0/repository/javax/transaction/javax.transaction-api/1.3/javax.transaction-api-1.3.jar:/soft/apache-maven-3.5.0/repository/javax/xml/bind/jaxb-api/2.3.1/jaxb-api-2.3.1.jar:/soft/apache-maven-3.5.0/repository/javax/activation/javax.activation-api/1.2.0/javax.activation-api-1.2.0.jar:/soft/apache-maven-3.5.0/repository/org/hibernate/hibernate-core/5.3.10.Final/hibernate-core-5.3.10.Final.jar:/soft/apache-maven-3.5.0/repository/org/jboss/logging/jboss-logging/3.3.2.Final/jboss-logging-3.3.2.Final.jar:/soft/apache-maven-3.5.0/repository/javax/persistence/javax.persistence-api/2.2/javax.persistence-api-2.2.jar:/soft/apache-maven-3.5.0/repository/org/javassist/javassist/3.23.2-GA/javassist-3.23.2-GA.jar:/soft/apache-maven-3.5.0/repository/net/bytebuddy/byte-buddy/1.9.16/byte-buddy-1.9.16.jar:/soft/apache-maven-3.5.0/repository/antlr/antlr/2.7.7/antlr-2.7.7.jar:/soft/apache-maven-3.5.0/repository/org/jboss/jandex/2.0.5.Final/jandex-2.0.5.Final.jar:/soft/apache-maven-3.5.0/repository/com/fasterxml/classmate/1.4.0/classmate-1.4.0.jar:/soft/apache-maven-3.5.0/repository/org/dom4j/dom4j/2.1.1/dom4j-2.1.1.jar:/soft/apache-maven-3.5.0/repository/org/hibernate/common/hibernate-commons-annotations/5.0.4.Final/hibernate-commons-annotations-5.0.4.Final.jar:/soft/apache-maven-3.5.0/repository/org/springframework/data/spring-data-jpa/2.1.10.RELEASE/spring-data-jpa-2.1.10.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/data/spring-data-commons/2.1.10.RELEASE/spring-data-commons-2.1.10.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/spring-orm/5.1.9.RELEASE/spring-orm-5.1.9.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/spring-tx/5.1.9.RELEASE/spring-tx-5.1.9.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/slf4j/slf4j-api/1.7.26/slf4j-api-1.7.26.jar:/soft/apache-maven-3.5.0/repository/org/springframework/spring-aspects/5.1.9.RELEASE/spring-aspects-5.1.9.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-starter-thymeleaf/2.1.7.RELEASE/spring-boot-starter-thymeleaf-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/thymeleaf/thymeleaf-spring5/3.0.11.RELEASE/thymeleaf-spring5-3.0.11.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/thymeleaf/thymeleaf/3.0.11.RELEASE/thymeleaf-3.0.11.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/attoparser/attoparser/2.0.5.RELEASE/attoparser-2.0.5.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/unbescape/unbescape/1.1.6.RELEASE/unbescape-1.1.6.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/thymeleaf/extras/thymeleaf-extras-java8time/3.0.4.RELEASE/thymeleaf-extras-java8time-3.0.4.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-starter-web/2.1.7.RELEASE/spring-boot-starter-web-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-starter-json/2.1.7.RELEASE/spring-boot-starter-json-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/com/fasterxml/jackson/core/jackson-databind/2.9.9/jackson-databind-2.9.9.jar:/soft/apache-maven-3.5.0/repository/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0.jar:/soft/apache-maven-3.5.0/repository/com/fasterxml/jackson/core/jackson-core/2.9.9/jackson-core-2.9.9.jar:/soft/apache-maven-3.5.0/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.9.9/jackson-datatype-jdk8-2.9.9.jar:/soft/apache-maven-3.5.0/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.9.9/jackson-datatype-jsr310-2.9.9.jar:/soft/apache-maven-3.5.0/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.9.9/jackson-module-parameter-names-2.9.9.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-starter-tomcat/2.1.7.RELEASE/spring-boot-starter-tomcat-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.22/tomcat-embed-core-9.0.22.jar:/soft/apache-maven-3.5.0/repository/org/apache/tomcat/embed/tomcat-embed-el/9.0.22/tomcat-embed-el-9.0.22.jar:/soft/apache-maven-3.5.0/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.22/tomcat-embed-websocket-9.0.22.jar:/soft/apache-maven-3.5.0/repository/org/hibernate/validator/hibernate-validator/6.0.17.Final/hibernate-validator-6.0.17.Final.jar:/soft/apache-maven-3.5.0/repository/javax/validation/validation-api/2.0.1.Final/validation-api-2.0.1.Final.jar:/soft/apache-maven-3.5.0/repository/org/springframework/spring-web/5.1.9.RELEASE/spring-web-5.1.9.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/spring-webmvc/5.1.9.RELEASE/spring-webmvc-5.1.9.RELEASE.jar:/soft/apache-maven-3.5.0/repository/mysql/mysql-connector-java/8.0.17/mysql-connector-java-8.0.17.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-starter-test/2.1.7.RELEASE/spring-boot-starter-test-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-test/2.1.7.RELEASE/spring-boot-test-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-test-autoconfigure/2.1.7.RELEASE/spring-boot-test-autoconfigure-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/com/jayway/jsonpath/json-path/2.4.0/json-path-2.4.0.jar:/soft/apache-maven-3.5.0/repository/net/minidev/json-smart/2.3/json-smart-2.3.jar:/soft/apache-maven-3.5.0/repository/net/minidev/accessors-smart/1.2/accessors-smart-1.2.jar:/soft/apache-maven-3.5.0/repository/org/ow2/asm/asm/5.0.4/asm-5.0.4.jar:/soft/apache-maven-3.5.0/repository/junit/junit/4.12/junit-4.12.jar:/soft/apache-maven-3.5.0/repository/org/assertj/assertj-core/3.11.1/assertj-core-3.11.1.jar:/soft/apache-maven-3.5.0/repository/org/mockito/mockito-core/2.23.4/mockito-core-2.23.4.jar:/soft/apache-maven-3.5.0/repository/net/bytebuddy/byte-buddy-agent/1.9.16/byte-buddy-agent-1.9.16.jar:/soft/apache-maven-3.5.0/repository/org/objenesis/objenesis/2.6/objenesis-2.6.jar:/soft/apache-maven-3.5.0/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/soft/apache-maven-3.5.0/repository/org/hamcrest/hamcrest-library/1.3/hamcrest-library-1.3.jar:/soft/apache-maven-3.5.0/repository/org/skyscreamer/jsonassert/1.5.0/jsonassert-1.5.0.jar:/soft/apache-maven-3.5.0/repository/com/vaadin/external/google/android-json/0.0.20131108.vaadin1/android-json-0.0.20131108.vaadin1.jar:/soft/apache-maven-3.5.0/repository/org/springframework/spring-core/5.1.9.RELEASE/spring-core-5.1.9.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/spring-jcl/5.1.9.RELEASE/spring-jcl-5.1.9.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/spring-test/5.1.9.RELEASE/spring-test-5.1.9.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/xmlunit/xmlunit-core/2.6.3/xmlunit-core-2.6.3.jar:/soft/apache-maven-3.5.0/repository/org/springframework/security/spring-security-test/5.1.6.RELEASE/spring-security-test-5.1.6.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/security/spring-security-core/5.1.6.RELEASE/spring-security-core-5.1.6.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-devtools/2.1.7.RELEASE/spring-boot-devtools-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot/2.1.7.RELEASE/spring-boot-2.1.7.RELEASE.jar:/soft/apache-maven-3.5.0/repository/org/springframework/boot/spring-boot-autoconfigure/2.1.7.RELEASE/spring-boot-autoconfigure-2.1.7.RELEASE.jar, user.name=micocube, java.vm.specification.version=1.8, sun.java.command=com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.micocube.web.admin.Lambda,test, java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre, sun.arch.data.model=64, user.language=zh, java.specification.vendor=Oracle Corporation, user.language.format=en, awt.toolkit=sun.lwawt.macosx.LWCToolkit, java.vm.info=mixed mode, java.version=1.8.0_131, java.ext.dirs=/Users/micocube/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java, sun.boot.class.path=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/classes, java.vendor=Oracle Corporation, file.separator=/, java.vendor.url.bug=http://bugreport.sun.com/bugreport/, idea.test.cyclic.buffer.size=1048576, sun.io.unicode.encoding=UnicodeBig, sun.cpu.endian=little, sun.cpu.isalist=}
*/
System.out.println(properties);
/**
* Predicate: 断言型接口,检测入参是否符合条件(符合则返回true)
* boolean test(T t);
*/
Predicate> test = str->str.size()>5;
/**
* 结果:
* false
*/
boolean testRs = test.test(Arrays.asList("a", "b", "c"));
System.out.println(testRs);
/**
* Function: 函数型接口,接收参数,返回结果
* R apply(T t);
*/
Function timesTen = a -> a*10;
/**
* 结果:100
*/
System.out.println(timesTen.apply(10));
BiFunction add = (a,b) -> a+b;
/**
* 结果:3
*/
System.out.println(add.apply(1,2));
Optional reduce = Stream.of("Hello", "World").map(s -> s + "@").reduce((a,b) -> a +b);
String s = reduce.orElse("empty");
/**
* 结果:Hello@World@
*/
System.out.println(s);
String reduce2 = Stream.of("Hello", "World").map(s2 -> s2 + "@").reduce("InitValue+",(a,b) -> a +b);
/**
* 结果:InitValue+Hello@World@
*/
System.out.println(reduce2);
// 第一个参数identity用于保存累加结果,第二个参数accumulator是累加参数方式,
// 第三个参数combiner用于计算两个accumulator得到的值,用于parallel并行计算,
// 串行计算并不会使用这个表达式
Integer parallelInteger = Stream.of('a', 'b', 'h', 'c').parallel().map(var -> {
System.out.println("char: "+var + ",int value:"+(int) var);
return (int) var;
}).reduce(0, (sum, var) -> sum + var, (sum1, sum2) -> {
System.out.println("sum1:"+sum1+",sum2:"+sum2+",sum:"+(int)(sum1+sum2));
return sum1 + sum2;
});
/**
* 结果:398
* 中间输出:
* char: b,int value:98
* char: a,int value:97
* char: c,int value:99
* sum1:97,sum2:98,sum:195
* char: h,int value:104
* sum1:104,sum2:99,sum:203
* sum1:195,sum2:203,sum:398
* 398
*/
System.out.println(parallelInteger);
Integer integer = Stream.of('a', 'b', 'h', 'c').map(var -> {
System.out.println("char: "+var + ",int value:"+(int) var);
return (int) var;
}).reduce(0, (sum, var) -> sum + var, (sum1, sum2) -> {
System.out.println("sum1:"+sum1+",sum2:"+sum2+",sum:"+(int)(sum1+sum2));
return sum1 + sum2;
});
/**
* 结果:398
* 中间输出:
* char: a,int value:97
* char: b,int value:98
* char: h,int value:104
* char: c,int value:99
* 398
*/
System.out.println(integer);
Optional integer3 = Stream.of('a', 'b', 'h', 'c').map(var -> {
System.out.println("char: "+var + ",int value:"+(int) var);
return (int) var;
}).reduce((sum, var) -> sum + var);
/**
* 结果:398
* char: a,int value:97
* char: b,int value:98
* char: h,int value:104
* char: c,int value:99
* 398
*/
System.out.println(integer3.orElse(0));
}
}