Jdk8 新特性 stream 流式數(shù)據(jù)處理

  • jdk9都出來了旺上,我還在看jdk8
  • 流式數(shù)據(jù)處理
      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 流式數(shù)據(jù)處理相關(guān)例子
       **/
      public class StreamApi {
          public static void main(String[] args) {
      
      
              //初始化
              List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1);
      
      
              //過濾窃这,收集所有偶數(shù)
              List<Integer> collect = integerList.stream()
                      .filter(integer -> integer % 2 == 0)
                      .collect(Collectors.toList());
      
      
              System.out.println("過濾杭攻,收集所有偶數(shù):" + 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);
      
      
              //計算數(shù)量
              long count = integerList.stream().count();
              System.out.println("計算數(shù)量" + count);
      
              Optional<Integer> collect8 = integerList.stream().collect(Collectors.maxBy((x1, x2) -> x1 - x2));
              Optional<Integer> 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<Integer> 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);
      
      
              //一次性得到元素個數(shù)、總和锅睛、均值现拒、最大值印蔬、最小值
              IntSummaryStatistics collect12 = integerList.stream().collect(Collectors.summarizingInt(Integer::intValue));
      
              System.out.println("一次性得到元素個數(shù)扛点、總和陵究、均值铜邮、最大值松蒜、最小值:" + collect12);
      
              //分組
              Map<Integer, List<Integer>> collect15 = integerList.stream().collect(
                      Collectors.groupingBy(Integer::intValue)
      
              );
              System.out.println("分組:" + collect15);
      
              Map<Integer, Long> collect14 = integerList.stream().collect(
                      Collectors.groupingBy(Integer::intValue, Collectors.counting())
              );
              System.out.println("可以有多級分組:" + collect14);
      
              //分區(qū)可以看做是分組的一種特殊情況秸苗,在分區(qū)中key只有兩種情況:true或false
              Map<Boolean, List<Integer>> collect16 = integerList.stream().collect(Collectors.partitioningBy(x -> x >= 7));
      
              System.out.println("分區(qū)可以看做是分組的一種特殊情況玖瘸,在分區(qū)中key只有兩種情況:true或false:" + collect16);
      
              //去重
              List<Integer> collect1 = integerList
                      .stream()
                      .distinct()
                      .collect(Collectors.toList());
              System.out.println("去重:" + collect1);
      
      
              //limit返回包含前n個元素的流
              List<Integer> collect2 = integerList
                      .stream()
                      .filter(integer -> integer % 2 == 0).limit(2)
                      .collect(Collectors.toList());
              System.out.println("limit:" + collect2);
      
      
              //排序,倒序排序
              List<Integer> collect3 = integerList.
                      stream()
                      .sorted((s1, s2) -> s2 - s1)
                      .collect(Collectors.toList());
              System.out.println("排序:" + collect3);
      
              //跳過前n個元素
              List<Integer> 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<String[]> collect5 = Arrays.stream(strs)
                      .map(s -> s.split(""))//將字符串映射成字符數(shù)組
                      .collect(Collectors.toList());
      
              System.out.println("將字符串映射成字符數(shù)組:" + collect5);
      
              //flatMap是將一個流中的每個值都轉(zhuǎn)成一個個流,然后再將這些流扁平化成為一個流
              List<String> collect7 = Arrays.stream(strs)
                      .map(s -> s.split(""))//每個字符串映射成string[]
                      .flatMap(Arrays::stream)//flatMap將由map映射得到的Stream<String[]>弧可,轉(zhuǎn)換成由各個字符串數(shù)組映射成的流Stream<String>
                      .collect(Collectors.toList());
              System.out.println("flatMap是將一個流中的每個值都轉(zhuǎn)成一個個流裁良,然后再將這些流扁平化成為一個流:" + collect7);
      
      
              //多個字符串將各個字符拆開价脾,去重
              List<String> collect6 = Arrays.stream(strs)
                      .map(s -> s.split(""))
                      .flatMap(Arrays::stream)
                      .distinct()
                      .collect(Collectors.toList());
      
              System.out.println("多個字符串將各個字符拆開彼棍,去重:" + collect6);
      
      
              //allMatch,檢測是否全部都滿足指定的參數(shù)行為
              boolean b = integerList.stream().allMatch(integer -> integer > 5);
              System.out.println("allMatch,檢測是否全部都滿足指定的參數(shù)行為:" + b);
      
              //anyMatch,檢測是否存在一個或多個滿足指定的參數(shù)行為
              boolean any = integerList.stream().anyMatch(integer -> integer > 5);
              System.out.println("anyMatch,檢測是否存在一個或多個滿足指定的參數(shù)行為:" + any);
      
              //nonMatch 檢測是否不存在滿足指定行為的元素
              boolean non = integerList.stream().noneMatch(integer -> integer > 5);
              System.out.println("nonMatch 檢測是否不存在滿足指定行為的元素:" + non);
      
              //用于返回滿足條件的第一個元素
      
              Optional<Integer> first = integerList.stream().filter(integer -> integer > 6).findFirst();
              if (first.isPresent()) System.out.println("用于返回滿足條件的第一個元素:" + first.get());
      
              //findAny相對于findFirst的區(qū)別在于,findAny不一定返回第一個涕蜂,而是返回任意一個
              //實際上對于順序流式處理而言机隙,findFirst和findAny返回的結(jié)果是一樣的有鹿,
              // 至于為什么會這樣設(shè)計葱跋,當我們啟用并行流式處理的時候娱俺,查找第一個元素往往會有很多限制,如果不是特別需求油宜,
              // 在并行流式處理中使用findAny的性能要比findFirst好。
              Optional<Integer> any1 = integerList.stream().filter(integer -> integer > 1).distinct().findAny();
              if (first.isPresent()) System.out.println("findAny不一定返回第一個顶吮,而是返回任意一個:" + any1.get());
              //?啟動并行流式處理雖然簡單悴了,只需要將stream()替換成parallelStream()即可湃交,
              // 但既然是并行搞莺,就會涉及到多線程安全問題才沧,所以在啟用之前要先確認并行是否值得
              // (并行的效率不一定高于順序執(zhí)行)温圆,另外就是要保證線程安全岁歉。此兩項無法保證锅移,
              // 那么并行毫無意義非剃,畢竟結(jié)果比速度更加重
              Optional<Integer> any2 = integerList.parallelStream().filter(integer -> integer > 1).distinct().findAny();
              if(any2.isPresent())System.out.println("并行流式處理:"+any2.get());
      
      
          }
      
      }
    
    

四大核心函數(shù)式接口Function、Consumer疯坤、Supplier深浮、Predicate

Function<T, R>

  • T:入?yún)㈩愋途保琑:出參類型

  • 調(diào)用方法:R apply(T t);

  • 定義函數(shù)示例:Function<Integer, Integer> func = p -> p * 10; // 輸出入?yún)⒌?0倍

  • 調(diào)用函數(shù)示例:func.apply(10); // 結(jié)果100

Consumer<T>

  • T:入?yún)㈩愋陀耆茫粵]有出參

  • 調(diào)用方法:void accept(T t);

  • 定義函數(shù)示例:Consumer<String> consumer= p -> System.out.println(p); // 因為沒有出參崔挖,常用于打印狸相、發(fā)送短信等消費動作

  • 調(diào)用函數(shù)示例:consumer.accept("18800008888");

Supplier<T>

  • T:出參類型脓鹃;沒有入?yún)?/p>

  • 調(diào)用方法:T get();

  • 定義函數(shù)示例:Supplier<Integer> supplier= () -> 100; // 常用于業(yè)務(wù)“有條件運行”時,符合條件再調(diào)用獲取結(jié)果的應(yīng)用場景岩齿;運行結(jié)果須提前定義栋齿,但不運行襟诸。

  • 調(diào)用函數(shù)示例:supplier.get();

Predicate<T>

  • T:入?yún)㈩愋透枨祝怀鰠㈩愋褪荁oolean

  • 調(diào)用方法:boolean test(T t);

  • 定義函數(shù)示例:Predicate<Integer> predicate = p -> p % 2 == 0; // 判斷是否、是不是偶數(shù)

  • 調(diào)用函數(shù)示例:predicate.test(100); // 運行結(jié)果true

Map(發(fā)散)和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: ldscube@gmail.com
 * createTime: 2019-09-25 14:16
 * version: 0.1
 * description:
 */
public class Lambda {
    @Test
    public void test(){

        /**
         *
         * Consumer<T>消費型接口悍缠,接收數(shù)據(jù)并處理
         *         void accept(T t);
         */
        Consumer<String> println = System.out::println;
        /**
         * 與上面的意義相同
         * Consumer<String> println2 = param -> System.out.println(param);
         * 結(jié)果:
         * abc
         */
        println.accept("abc");




        /**
         *Supplier<T>: 供給型接口,對外提供數(shù)據(jù)
         *         T get()
         */
        Supplier<Properties> getProperties = System::getProperties;
        Properties properties = getProperties.get();
        /**
         * 結(jié)果:
         * {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<T>: 斷言型接口炼绘,檢測入?yún)⑹欠穹蠗l件(符合則返回true)
         *         boolean test(T t);
         */
        Predicate<List<String>> test = str->str.size()>5;
        /**
         * 結(jié)果:
         * false
         */
        boolean testRs = test.test(Arrays.asList("a", "b", "c"));
        System.out.println(testRs);




        /**
         * Function<T, R>: 函數(shù)型接口俺亮,接收參數(shù),返回結(jié)果
         *         R apply(T t);
         */
        Function<Integer,Integer> timesTen = a -> a*10;
        /**
         * 結(jié)果:100
         */
        System.out.println(timesTen.apply(10));


        BiFunction<Integer,Integer,Integer> add = (a,b) -> a+b;
        /**
         * 結(jié)果:3
         */
        System.out.println(add.apply(1,2));


        Optional<String> reduce = Stream.of("Hello", "World").map(s -> s + "@").reduce((a,b) -> a +b);
        String s = reduce.orElse("empty");
        /**
         * 結(jié)果:Hello@World@
         */
        System.out.println(s);

        String reduce2 = Stream.of("Hello", "World").map(s2 -> s2 + "@").reduce("InitValue+",(a,b) -> a +b);
        /**
         * 結(jié)果:InitValue+Hello@World@
         */
        System.out.println(reduce2);


        // 第一個參數(shù)identity用于保存累加結(jié)果疟呐,第二個參數(shù)accumulator是累加參數(shù)方式脚曾,
        // 第三個參數(shù)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;
        });
        /**
         * 結(jié)果: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;
        });

        /**
         * 結(jié)果: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<Integer> 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);
        /**
         * 結(jié)果: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));

    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末本讥,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子鲁冯,更是在濱河造成了極大的恐慌拷沸,老刑警劉巖薯演,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件衡创,死亡現(xiàn)場離奇詭異哟玷,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人篷店,你說我怎么就攤上這事蹄殃。” “怎么了?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵卿堂,是天一觀的道長。 經(jīng)常有香客問我,道長怀各,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任法焰,我火速辦了婚禮乙濒,結(jié)果婚禮上毙玻,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好该互,可當我...
    茶點故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般梆掸。 火紅的嫁衣襯著肌膚如雪嬉挡。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天河爹,我揣著相機與錄音魔眨,去河邊找鬼。 笑死,一個胖子當著我的面吹牛墓毒,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼损肛!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎香追,沒想到半個月后税弃,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體遗增,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年铣除,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辑鲤。...
    茶點故事閱讀 40,912評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡决左,死狀恐怖挚躯,靈堂內(nèi)的尸體忽然破棺而出越败,到底是詐尸還是另有隱情,我是刑警寧澤半哟,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布译打,位于F島的核電站竿刁,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏队腐。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一盐捷、第九天 我趴在偏房一處隱蔽的房頂上張望环疼。 院中可真熱鬧,春花似錦锰霜、人聲如沸屡立。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽挨措。三九已至惧盹,卻和暖如春钧椰,著一層夾襖步出監(jiān)牢的瞬間诊沪,已是汗流浹背渐裸。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留怠晴,地道東北人蒜田。 一個月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓稿械,卻偏偏與公主長得像,于是被迫代替她去往敵國和親冲粤。 傳聞我的和親對象是個殘疾皇子美莫,可洞房花燭夜當晚...
    茶點故事閱讀 45,922評論 2 361

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)梯捕,斷路器厢呵,智...
    卡卡羅2017閱讀 134,716評論 18 139
  • 轉(zhuǎn)自: Java 8 中的 Streams API 詳解 為什么需要 Stream Stream 作為 Java ...
    普度眾生的面癱青年閱讀 2,921評論 0 11
  • Java 8自Java 5(發(fā)行于2004)以來最具革命性的版本。Java 8 為Java語言傀顾、編譯器襟铭、類庫、開發(fā)...
    誰在烽煙彼岸閱讀 891評論 0 4
  • Java 8自Java 5(發(fā)行于2004)以來最具革命性的版本锣笨。Java 8 為Java語言蝌矛、編譯器、類庫错英、開發(fā)...
    huoyl0410閱讀 640評論 1 2
  • 投射孩子自控力越來越強,投射孩子晚上睡的香隆豹,身體健康棒棒噠椭岩,投射孩子能處理好學習和游戲的關(guān)系,能盡快從游戲中出來璃赡,...
    a蘇州東山錦湖之家張金成閱讀 141評論 0 3