jdk8 新特性stream()方法理解与使用

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: stream() 对象集合取出一个字段成新集合1、// 采集门店属性项

stream() 对象集合取出一个字段成新集合

1、

// 采集门店属性项

    List<CollectionShopAttr> shopAttrList = collectionShopService
            .listShopAttrByShopId(collectShopDetailReq.getId());

对象集合取出一个字段成新集合

//取出采集项attrKey为集合

    List<String> attrKeyList = shopAttrList.stream().map(CollectionShopAttr::getAttrKey).collect(Collectors.toList());

2、

List<CollectionTemplateContent> contentList = collectionTemplateService
                .listContentByTemplateIdAndType(detail.getType(), detail.getMerchantId());
List<String> tempKeyList =
                    contentList.stream().map(CollectionTemplateContent::getCode).collect(Collectors.toList());

IO流用于读写,Stream流用于处理数组和集合数据,解决传统集合遍历。filter()方法用于过滤,forEach+sout循环输出结果。

@Test
    public void streamTest() {
        /**
         * IO流用于读写,Stream流用于处理数组和集合数据,解决传统集合遍历
         */
        Set<String> set = new TreeSet<>();
        set.add("张**");
        set.add("wang**");
        set.add("张*1");
        set.add("张wagn**");
        set.add("kak**");
        set.add("lili**");
        set.add("张**");
        //forEach
        set.stream().filter(name -> name.startsWith("张"))
                .filter(name -> name.length() == 3)
                .forEach(name -> System.out.println(name));
    }

初始化组装map,map输出,用keySet方法获得key集合。

    @Data
    class Entity {
        Integer id;
        String username;
    }
    @Test
    public void keySetTest() {
        /**
         * keySet
         */
        //初始化list
        List<Entity> list = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            Entity object = new Entity();
            object.setId(i);
            object.setUsername(String.valueOf(i >>> 1));
            log.info("i:{}, i >>> 1:{}", i, i >>> 1);
            list.add(object);
        }
        //初始化map
        Map<Integer, String> map = new TreeMap<>();
        if (CollectionUtil.isNotEmpty(list)) {
            map = list.stream().collect(Collectors.toMap(Entity::getId, Entity::getUsername,
                    (t1, t2) -> t2));
            log.info("map:{}", map);
        }
        //map输出
        if (null != map) {
            Map<Integer, String> finalMap = map;
            map.keySet().forEach(item -> {
                System.out.println("key = " + item + ", value = " + finalMap.get(item));
            });
            Set<Integer> set = map.keySet();
            System.out.println(set);
        }
    }

以上两个输出结果

"C:\Program Files\Java\jdk1.8.0_131\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:E:\software\IntelliJ IDEA 2017.2.5\lib\idea_rt.jar=62217:E:\software\IntelliJ IDEA 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath "E:\software\IntelliJ IDEA 2017.2.5\lib\idea_rt.jar;E:\software\IntelliJ IDEA 2017.2.5\plugins\junit\lib\junit-rt.jar;E:\software\IntelliJ IDEA 2017.2.5\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;D:\code\saas-collect\saas-collect-server\target\test-classes;D:\code\saas-collect\saas-collect-server\target\classes;D:\code\saas-collect\saas-collect-api\target\classes;D:\maven\repository\com\lego\lego-common-core\1.0.11\lego-common-core-1.0.11.jar;D:\maven\repository\com\lego\lego-common-verify\1.0.0\lego-common-verify-1.0.0.jar;D:\maven\repository\cn\hutool\hutool-all\5.2.5\hutool-all-5.2.5.jar;D:\maven\repository\org\apache\commons\commons-lang3\3.9\commons-lang3-3.9.jar;D:\maven\repository\com\github\ulisesbocchio\jasypt-spring-boot-starter\2.1.0\jasypt-spring-boot-starter-2.1.0.jar;D:\maven\repository\com\github\ulisesbocchio\jasypt-spring-boot\2.1.0\jasypt-spring-boot-2.1.0.jar;D:\maven\repository\org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar;D:\maven\repository\javax\servlet\javax.servlet-api\4.0.1\javax.servlet-api-4.0.1.jar;D:\maven\repository\com\baomidou\mybatis-plus-extension\3.3.1\mybatis-plus-extension-3.3.1.jar;D:\maven\repository\com\baomidou\mybatis-plus-core\3.3.1\mybatis-plus-core-3.3.1.jar;D:\maven\repository\com\baomidou\mybatis-plus-annotation\3.3.1\mybatis-plus-annotation-3.3.1.jar;D:\maven\repository\com\github\jsqlparser\jsqlparser\3.1\jsqlparser-3.1.jar;D:\maven\repository\org\mybatis\mybatis\3.5.3\mybatis-3.5.3.jar;D:\maven\repository\org\mybatis\mybatis-spring\2.0.3\mybatis-spring-2.0.3.jar;D:\maven\repository\io\github\openfeign\feign-okhttp\10.7.4\feign-okhttp-10.7.4.jar;D:\maven\repository\com\squareup\okhttp3\okhttp\3.14.7\okhttp-3.14.7.jar;D:\maven\repository\com\squareup\okio\okio\1.17.2\okio-1.17.2.jar;D:\maven\repository\org\springframework\cloud\spring-cloud-starter-netflix-hystrix\2.2.2.RELEASE\spring-cloud-starter-netflix-hystrix-2.2.2.RELEASE.jar;D:\maven\repository\org\springframework\cloud\spring-cloud-netflix-hystrix\2.2.2.RELEASE\spring-cloud-netflix-hystrix-2.2.2.RELEASE.jar;D:\maven\repository\com\netflix\hystrix\hystrix-core\1.5.18\hystrix-core-1.5.18.jar;D:\maven\repository\com\netflix\hystrix\hystrix-serialization\1.5.18\hystrix-serialization-1.5.18.jar;D:\maven\repository\com\fasterxml\jackson\module\jackson-module-afterburner\2.10.3\jackson-module-afterburner-2.10.3.jar;D:\maven\repository\com\netflix\hystrix\hystrix-metrics-event-stream\1.5.18\hystrix-metrics-event-stream-1.5.18.jar;D:\maven\repository\com\netflix\hystrix\hystrix-javanica\1.5.18\hystrix-javanica-1.5.18.jar;D:\maven\repository\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;D:\maven\repository\io\reactivex\rxjava-reactive-streams\1.2.1\rxjava-reactive-streams-1.2.1.jar;D:\maven\repository\org\reactivestreams\reactive-streams\1.0.3\reactive-streams-1.0.3.jar;D:\maven\repository\org\springframework\boot\spring-boot-starter-validation\2.2.6.RELEASE\spring-boot-starter-validation-2.2.6.RELEASE.jar;D:\maven\repository\jakarta\validation\jakarta.validation-api\2.0.2\jakarta.validation-api-2.0.2.jar;D:\maven\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.33\tomcat-embed-el-9.0.33.jar;D:\maven\repository\org\hibernate\validator\hibernate-validator\6.0.18.Final\hibernate-validator-6.0.18.Final.jar;D:\maven\repository\org\springframework\boot\spring-boot-starter-json\2.2.6.RELEASE\spring-boot-starter-json-2.2.6.RELEASE.jar;D:\maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.10.3\jackson-datatype-jdk8-2.10.3.jar;D:\maven\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.10.3\jackson-module-parameter-names-2.10.3.jar;D:\maven\repository\io\swagger\swagger-annotations\1.5.24\swagger-annotations-1.5.24.jar;D:\maven\repository\com\alibaba\fastjson\1.2.68\fastjson-1.2.68.jar;D:\maven\repository\com\dyuproject\protostuff\protostuff-core\1.1.3\protostuff-core-1.1.3.jar;D:\maven\repository\com\dyuproject\protostuff\protostuff-api\1.1.3\protostuff-api-1.1.3.jar;D:\maven\repository\com\dyuproject\protostuff\protostuff-runtime\1.1.3\protostuff-runtime-1.1.3.jar;D:\maven\repository\com\dyuproject\protostuff\protostuff-collectionschema\1.1.3\protostuff-collectionschema-1.1.3.jar;D:\maven\repository\org\apache\skywalking\apm-toolkit-trace\8.0.1\apm-toolkit-trace-8.0.1.jar;D:\maven\repository\com\lego\lego-common-mybatis\1.0.4\lego-common-mybatis-1.0.4.jar;D:\maven\repository\com\baomidou\mybatis-plus-boot-starter\3.3.1\mybatis-plus-boot-starter-3.3.1.jar;D:\maven\repository\com\baomidou\mybatis-plus\3.3.1\mybatis-plus-3.3.1.jar;D:\maven\repository\org\springframework\boot\spring-boot-starter-jdbc\2.2.6.RELEASE\spring-boot-starter-jdbc-2.2.6.RELEASE.jar;D:\maven\repository\com\zaxxer\HikariCP\3.4.2\HikariCP-3.4.2.jar;D:\maven\repository\org\springframework\spring-jdbc\5.2.5.RELEASE\spring-jdbc-5.2.5.RELEASE.jar;D:\maven\repository\mysql\mysql-connector-java\8.0.19\mysql-connector-java-8.0.19.jar;D:\maven\repository\com\lego\lego-common-swagger\1.1.1\lego-common-swagger-1.1.1.jar;D:\maven\repository\io\springfox\springfox-swagger2\2.9.2\springfox-swagger2-2.9.2.jar;D:\maven\repository\io\swagger\swagger-models\1.5.24\swagger-models-1.5.24.jar;D:\maven\repository\com\fasterxml\jackson\core\jackson-annotations\2.10.3\jackson-annotations-2.10.3.jar;D:\maven\repository\io\springfox\springfox-spi\2.9.2\springfox-spi-2.9.2.jar;D:\maven\repository\io\springfox\springfox-core\2.9.2\springfox-core-2.9.2.jar;D:\maven\repository\io\springfox\springfox-schema\2.9.2\springfox-schema-2.9.2.jar;D:\maven\repository\io\springfox\springfox-swagger-common\2.9.2\springfox-swagger-common-2.9.2.jar;D:\maven\repository\io\springfox\springfox-spring-web\2.9.2\springfox-spring-web-2.9.2.jar;D:\maven\repository\com\google\guava\guava\28.2-android\guava-28.2-android.jar;D:\maven\repository\com\google\guava\failureaccess\1.0.1\failureaccess-1.0.1.jar;D:\maven\repository\com\google\guava\listenablefuture\9999.0-empty-to-avoid-conflict-with-guava\listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;D:\maven\repository\com\google\code\findbugs\jsr305\3.0.2\jsr305-3.0.2.jar;D:\maven\repository\org\checkerframework\checker-compat-qual\2.5.5\checker-compat-qual-2.5.5.jar;D:\maven\repository\com\google\errorprone\error_prone_annotations\2.3.4\error_prone_annotations-2.3.4.jar;D:\maven\repository\com\google\j2objc\j2objc-annotations\1.3\j2objc-annotations-1.3.jar;D:\maven\repository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;D:\maven\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\maven\repository\org\springframework\plugin\spring-plugin-core\1.2.0.RELEASE\spring-plugin-core-1.2.0.RELEASE.jar;D:\maven\repository\org\springframework\plugin\spring-plugin-metadata\1.2.0.RELEASE\spring-plugin-metadata-1.2.0.RELEASE.jar;D:\maven\repository\org\mapstruct\mapstruct\1.2.0.Final\mapstruct-1.2.0.Final.jar;D:\maven\repository\io\springfox\springfox-swagger-ui\2.9.2\springfox-swagger-ui-2.9.2.jar;D:\maven\repository\com\github\xiaoymin\knife4j-spring-boot-starter\2.0.3\knife4j-spring-boot-starter-2.0.3.jar;D:\maven\repository\com\github\xiaoymin\knife4j-spring-boot-autoconfigure\2.0.3\knife4j-spring-boot-autoconfigure-2.0.3.jar;D:\maven\repository\com\github\xiaoymin\knife4j-spring\2.0.3\knife4j-spring-2.0.3.jar;D:\maven\repository\com\github\xiaoymin\knife4j-annotations\2.0.3\knife4j-annotations-2.0.3.jar;D:\maven\repository\com\github\xiaoymin\knife4j-core\2.0.3\knife4j-core-2.0.3.jar;D:\maven\repository\io\springfox\springfox-bean-validators\2.9.2\springfox-bean-validators-2.9.2.jar;D:\maven\repository\com\github\xiaoymin\knife4j-spring-ui\2.0.3\knife4j-spring-ui-2.0.3.jar;D:\maven\repository\com\github\xiaoymin\knife4j-micro-spring-boot-starter\2.0.3\knife4j-micro-spring-boot-starter-2.0.3.jar;D:\maven\repository\org\springframework\boot\spring-boot-starter-web\2.2.6.RELEASE\spring-boot-starter-web-2.2.6.RELEASE.jar;D:\maven\repository\org\springframework\spring-web\5.2.5.RELEASE\spring-web-5.2.5.RELEASE.jar;D:\maven\repository\org\springframework\spring-webmvc\5.2.5.RELEASE\spring-webmvc-5.2.5.RELEASE.jar;D:\maven\repository\org\springframework\spring-expression\5.2.5.RELEASE\spring-expression-5.2.5.RELEASE.jar;D:\maven\repository\com\github\xiaoymin\swagger-bootstrap-ui\1.9.4\swagger-bootstrap-ui-1.9.4.jar;D:\maven\repository\org\javassist\javassist\3.25.0-GA\javassist-3.25.0-GA.jar;D:\maven\repository\com\lego\lego-common-cache\1.0.4\lego-common-cache-1.0.4.jar;D:\maven\repository\org\springframework\boot\spring-boot-starter-integration\2.2.6.RELEASE\spring-boot-starter-integration-2.2.6.RELEASE.jar;D:\maven\repository\org\springframework\integration\spring-integration-core\5.2.5.RELEASE\spring-integration-core-5.2.5.RELEASE.jar;D:\maven\repository\io\projectreactor\reactor-core\3.3.4.RELEASE\reactor-core-3.3.4.RELEASE.jar;D:\maven\repository\org\springframework\retry\spring-retry\1.2.5.RELEASE\spring-retry-1.2.5.RELEASE.jar;D:\maven\repository\org\springframework\spring-messaging\5.2.5.RELEASE\spring-messaging-5.2.5.RELEASE.jar;D:\maven\repository\org\springframework\spring-tx\5.2.5.RELEASE\spring-tx-5.2.5.RELEASE.jar;D:\maven\repository\org\springframework\integration\spring-integration-redis\5.2.6.RELEASE\spring-integration-redis-5.2.6.RELEASE.jar;D:\maven\repository\org\springframework\data\spring-data-redis\2.2.6.RELEASE\spring-data-redis-2.2.6.RELEASE.jar;D:\maven\repository\org\springframework\data\spring-data-keyvalue\2.2.6.RELEASE\spring-data-keyvalue-2.2.6.RELEASE.jar;D:\maven\repository\org\springframework\data\spring-data-commons\2.2.6.RELEASE\spring-data-commons-2.2.6.RELEASE.jar;D:\maven\repository\org\springframework\spring-context-support\5.2.5.RELEASE\spring-context-support-5.2.5.RELEASE.jar;D:\maven\repository\org\springframework\spring-beans\5.2.5.RELEASE\spring-beans-5.2.5.RELEASE.jar;D:\maven\repository\org\springframework\spring-context\5.2.5.RELEASE\spring-context-5.2.5.RELEASE.jar;D:\maven\repository\org\springframework\boot\spring-boot-starter-data-redis\2.2.6.RELEASE\spring-boot-starter-data-redis-2.2.6.RELEASE.jar;D:\maven\repository\io\lettuce\lettuce-core\5.2.2.RELEASE\lettuce-core-5.2.2.RELEASE.jar;D:\maven\repository\io\netty\netty-common\4.1.48.Final\netty-common-4.1.48.Final.jar;D:\maven\repository\io\netty\netty-handler\4.1.48.Final\netty-handler-4.1.48.Final.jar;D:\maven\repository\io\netty\netty-resolver\4.1.48.Final\netty-resolver-4.1.48.Final.jar;D:\maven\repository\io\netty\netty-buffer\4.1.48.Final\netty-buffer-4.1.48.Final.jar;D:\maven\repository\io\netty\netty-codec\4.1.48.Final\netty-codec-4.1.48.Final.jar;D:\maven\repository\io\netty\netty-transport\4.1.48.Final\netty-transport-4.1.48.Final.jar;D:\maven\repository\org\apache\commons\commons-pool2\2.4.2\commons-pool2-2.4.2.jar;D:\maven\repository\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-discovery\2.2.1.RELEASE\spring-cloud-starter-alibaba-nacos-discovery-2.2.1.RELEASE.jar;D:\maven\repository\com\alibaba\nacos\nacos-client\1.2.1\nacos-client-1.2.1.jar;D:\maven\repository\com\alibaba\nacos\nacos-common\1.2.1\nacos-common-1.2.1.jar;D:\maven\repository\commons-io\commons-io\2.2\commons-io-2.2.jar;D:\maven\repository\com\alibaba\nacos\nacos-api\1.2.1\nacos-api-1.2.1.jar;D:\maven\repository\commons-codec\commons-codec\1.13\commons-codec-1.13.jar;D:\maven\repository\com\fasterxml\jackson\core\jackson-core\2.10.3\jackson-core-2.10.3.jar;D:\maven\repository\com\fasterxml\jackson\core\jackson-databind\2.10.3\jackson-databind-2.10.3.jar;D:\maven\repository\io\prometheus\simpleclient\0.5.0\simpleclient-0.5.0.jar;D:\maven\repository\org\yaml\snakeyaml\1.25\snakeyaml-1.25.jar;D:\maven\repository\com\alibaba\spring\spring-context-support\1.0.6\spring-context-support-1.0.6.jar;D:\maven\repository\org\springframework\cloud\spring-cloud-commons\2.2.2.RELEASE\spring-cloud-commons-2.2.2.RELEASE.jar;D:\maven\repository\org\springframework\security\spring-security-crypto\5.2.2.RELEASE\spring-security-crypto-5.2.2.RELEASE.jar;D:\maven\repository\org\springframework\cloud\spring-cloud-context\2.2.2.RELEASE\spring-cloud-context-2.2.2.RELEASE.jar;D:\maven\repository\org\springframework\cloud\spring-cloud-starter-netflix-ribbon\2.2.2.RELEASE\spring-cloud-starter-netflix-ribbon-2.2.2.RELEASE.jar;D:\maven\repository\org\springframework\cloud\spring-cloud-starter\2.2.2.RELEASE\spring-cloud-starter-2.2.2.RELEASE.jar;D:\maven\repository\org\springframework\security\spring-security-rsa\1.0.9.RELEASE\spring-security-rsa-1.0.9.RELEASE.jar;D:\maven\repository\org\bouncycastle\bcpkix-jdk15on\1.64\bcpkix-jdk15on-1.64.jar;D:\maven\repository\org\bouncycastle\bcprov-jdk15on\1.64\bcprov-jdk15on-1.64.jar;D:\maven\repository\org\springframework\cloud\spring-cloud-starter-netflix-archaius\2.2.2.RELEASE\spring-cloud-starter-netflix-archaius-2.2.2.RELEASE.jar;D:\maven\repository\com\netflix\archaius\archaius-core\0.7.6\archaius-core-0.7.6.jar;D:\maven\repository\commons-configuration\commons-configuration\1.8\commons-configuration-1.8.jar;D:\maven\repository\com\netflix\ribbon\ribbon\2.3.0\ribbon-2.3.0.jar;D:\maven\repository\com\netflix\ribbon\ribbon-transport\2.3.0\ribbon-transport-2.3.0.jar;D:\maven\repository\io\reactivex\rxnetty-contexts\0.4.9\rxnetty-contexts-0.4.9.jar;D:\maven\repository\io\reactivex\rxnetty-servo\0.4.9\rxnetty-servo-0.4.9.jar;D:\maven\repository\javax\inject\javax.inject\1\javax.inject-1.jar;D:\maven\repository\io\reactivex\rxnetty\0.4.9\rxnetty-0.4.9.jar;D:\maven\repository\com\netflix\ribbon\ribbon-core\2.3.0\ribbon-core-2.3.0.jar;D:\maven\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\maven\repository\com\netflix\ribbon\ribbon-httpclient\2.3.0\ribbon-httpclient-2.3.0.jar;D:\maven\repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;D:\maven\repository\org\apache\httpcomponents\httpclient\4.5.12\httpclient-4.5.12.jar;D:\maven\repository\org\apache\httpcomponents\httpcore\4.4.13\httpcore-4.4.13.jar;D:\maven\repository\com\sun\jersey\jersey-client\1.19.1\jersey-client-1.19.1.jar;D:\maven\repository\com\sun\jersey\jersey-core\1.19.1\jersey-core-1.19.1.jar;D:\maven\repository\javax\ws\rs\jsr311-api\1.1.1\jsr311-api-1.1.1.jar;D:\maven\repository\com\sun\jersey\contribs\jersey-apache-client4\1.19.1\jersey-apache-client4-1.19.1.jar;D:\maven\repository\com\netflix\servo\servo-core\0.12.21\servo-core-0.12.21.jar;D:\maven\repository\com\netflix\netflix-commons\netflix-commons-util\0.3.0\netflix-commons-util-0.3.0.jar;D:\maven\repository\com\netflix\ribbon\ribbon-loadbalancer\2.3.0\ribbon-loadbalancer-2.3.0.jar;D:\maven\repository\com\netflix\netflix-commons\netflix-statistics\0.1.1\netflix-statistics-0.1.1.jar;D:\maven\repository\io\reactivex\rxjava\1.3.8\rxjava-1.3.8.jar;D:\maven\repository\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-config\2.2.1.RELEASE\spring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar;D:\maven\repository\org\springframework\boot\spring-boot-starter-undertow\2.2.6.RELEASE\spring-boot-starter-undertow-2.2.6.RELEASE.jar;D:\maven\repository\io\undertow\undertow-core\2.0.30.Final\undertow-core-2.0.30.Final.jar;D:\maven\repository\org\jboss\logging\jboss-logging\3.4.1.Final\jboss-logging-3.4.1.Final.jar;D:\maven\repository\org\jboss\xnio\xnio-api\3.3.8.Final\xnio-api-3.3.8.Final.jar;D:\maven\repository\org\jboss\xnio\xnio-nio\3.3.8.Final\xnio-nio-3.3.8.Final.jar;D:\maven\repository\io\undertow\undertow-servlet\2.0.30.Final\undertow-servlet-2.0.30.Final.jar;D:\maven\repository\org\jboss\spec\javax\annotation\jboss-annotations-api_1.2_spec\1.0.2.Final\jboss-annotations-api_1.2_spec-1.0.2.Final.jar;D:\maven\repository\io\undertow\undertow-websockets-jsr\2.0.30.Final\undertow-websockets-jsr-2.0.30.Final.jar;D:\maven\repository\org\jboss\spec\javax\websocket\jboss-websocket-api_1.1_spec\1.1.4.Final\jboss-websocket-api_1.1_spec-1.1.4.Final.jar;D:\maven\repository\jakarta\servlet\jakarta.servlet-api\4.0.3\jakarta.servlet-api-4.0.3.jar;D:\maven\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;D:\maven\repository\org\springframework\boot\spring-boot-starter-test\2.2.6.RELEASE\spring-boot-starter-test-2.2.6.RELEASE.jar;D:\maven\repository\org\springframework\boot\spring-boot-starter\2.2.6.RELEASE\spring-boot-starter-2.2.6.RELEASE.jar;D:\maven\repository\org\springframework\boot\spring-boot\2.2.6.RELEASE\spring-boot-2.2.6.RELEASE.jar;D:\maven\repository\org\springframework\boot\spring-boot-starter-logging\2.2.6.RELEASE\spring-boot-starter-logging-2.2.6.RELEASE.jar;D:\maven\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\maven\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\maven\repository\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar;D:\maven\repository\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar;D:\maven\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\maven\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\maven\repository\org\springframework\boot\spring-boot-test\2.2.6.RELEASE\spring-boot-test-2.2.6.RELEASE.jar;D:\maven\repository\org\springframework\boot\spring-boot-test-autoconfigure\2.2.6.RELEASE\spring-boot-test-autoconfigure-2.2.6.RELEASE.jar;D:\maven\repository\com\jayway\jsonpath\json-path\2.4.0\json-path-2.4.0.jar;D:\maven\repository\net\minidev\json-smart\2.3\json-smart-2.3.jar;D:\maven\repository\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;D:\maven\repository\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;D:\maven\repository\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;D:\maven\repository\org\junit\jupiter\junit-jupiter\5.5.2\junit-jupiter-5.5.2.jar;D:\maven\repository\org\junit\jupiter\junit-jupiter-api\5.5.2\junit-jupiter-api-5.5.2.jar;D:\maven\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;D:\maven\repository\org\junit\platform\junit-platform-commons\1.5.2\junit-platform-commons-1.5.2.jar;D:\maven\repository\org\junit\jupiter\junit-jupiter-params\5.5.2\junit-jupiter-params-5.5.2.jar;D:\maven\repository\org\junit\jupiter\junit-jupiter-engine\5.5.2\junit-jupiter-engine-5.5.2.jar;D:\maven\repository\org\junit\vintage\junit-vintage-engine\5.5.2\junit-vintage-engine-5.5.2.jar;D:\maven\repository\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;D:\maven\repository\org\junit\platform\junit-platform-engine\1.5.2\junit-platform-engine-1.5.2.jar;D:\maven\repository\junit\junit\4.12\junit-4.12.jar;D:\maven\repository\org\mockito\mockito-junit-jupiter\3.1.0\mockito-junit-jupiter-3.1.0.jar;D:\maven\repository\org\assertj\assertj-core\3.13.2\assertj-core-3.13.2.jar;D:\maven\repository\org\hamcrest\hamcrest\2.1\hamcrest-2.1.jar;D:\maven\repository\org\mockito\mockito-core\3.1.0\mockito-core-3.1.0.jar;D:\maven\repository\net\bytebuddy\byte-buddy\1.10.8\byte-buddy-1.10.8.jar;D:\maven\repository\net\bytebuddy\byte-buddy-agent\1.10.8\byte-buddy-agent-1.10.8.jar;D:\maven\repository\org\objenesis\objenesis\2.6\objenesis-2.6.jar;D:\maven\repository\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;D:\maven\repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;D:\maven\repository\org\springframework\spring-core\5.2.5.RELEASE\spring-core-5.2.5.RELEASE.jar;D:\maven\repository\org\springframework\spring-jcl\5.2.5.RELEASE\spring-jcl-5.2.5.RELEASE.jar;D:\maven\repository\org\springframework\spring-test\5.2.5.RELEASE\spring-test-5.2.5.RELEASE.jar;D:\maven\repository\org\xmlunit\xmlunit-core\2.6.4\xmlunit-core-2.6.4.jar;D:\maven\repository\org\freemarker\freemarker\2.3.30\freemarker-2.3.30.jar;D:\maven\repository\org\springframework\cloud\spring-cloud-openfeign-core\2.2.2.RELEASE\spring-cloud-openfeign-core-2.2.2.RELEASE.jar;D:\maven\repository\org\springframework\boot\spring-boot-autoconfigure\2.2.6.RELEASE\spring-boot-autoconfigure-2.2.6.RELEASE.jar;D:\maven\repository\org\springframework\cloud\spring-cloud-netflix-ribbon\2.2.2.RELEASE\spring-cloud-netflix-ribbon-2.2.2.RELEASE.jar;D:\maven\repository\org\springframework\cloud\spring-cloud-netflix-archaius\2.2.2.RELEASE\spring-cloud-netflix-archaius-2.2.2.RELEASE.jar;D:\maven\repository\org\springframework\boot\spring-boot-starter-aop\2.2.6.RELEASE\spring-boot-starter-aop-2.2.6.RELEASE.jar;D:\maven\repository\org\springframework\spring-aop\5.2.5.RELEASE\spring-aop-5.2.5.RELEASE.jar;D:\maven\repository\org\aspectj\aspectjweaver\1.9.5\aspectjweaver-1.9.5.jar;D:\maven\repository\io\github\openfeign\form\feign-form-spring\3.8.0\feign-form-spring-3.8.0.jar;D:\maven\repository\io\github\openfeign\form\feign-form\3.8.0\feign-form-3.8.0.jar;D:\maven\repository\commons-fileupload\commons-fileupload\1.4\commons-fileupload-1.4.jar;D:\maven\repository\org\springframework\boot\spring-boot-starter-actuator\2.2.6.RELEASE\spring-boot-starter-actuator-2.2.6.RELEASE.jar;D:\maven\repository\org\springframework\boot\spring-boot-actuator-autoconfigure\2.2.6.RELEASE\spring-boot-actuator-autoconfigure-2.2.6.RELEASE.jar;D:\maven\repository\org\springframework\boot\spring-boot-actuator\2.2.6.RELEASE\spring-boot-actuator-2.2.6.RELEASE.jar;D:\maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.10.3\jackson-datatype-jsr310-2.10.3.jar;D:\maven\repository\io\micrometer\micrometer-core\1.3.6\micrometer-core-1.3.6.jar;D:\maven\repository\org\hdrhistogram\HdrHistogram\2.1.11\HdrHistogram-2.1.11.jar;D:\maven\repository\org\latencyutils\LatencyUtils\2.0.3\LatencyUtils-2.0.3.jar;D:\maven\repository\com\hankcs\hanlp\portable-1.7.8\hanlp-portable-1.7.8.jar;D:\maven\repository\org\projectlombok\lombok\1.18.12\lombok-1.18.12.jar;D:\maven\repository\com\lego\lego-merchant-api\1.0.2-SNAPSHOT\lego-merchant-api-1.0.2-SNAPSHOT.jar;D:\maven\repository\com\lego\lego-data-api\1.4.2-SNAPSHOT\lego-data-api-1.4.2-20211221.102338-439.jar;D:\maven\repository\com\lego\saas-brand-api\1.0.0-SNAPSHOT\saas-brand-api-1.0.0-20220111.074747-39.jar;D:\maven\repository\com\lego\saas-report-api\1.0.0-SNAPSHOT\saas-report-api-1.0.0-SNAPSHOT.jar;D:\maven\repository\com\fuxiplus\fuxi-plus-shopmall-api\1.0.1-SNAPSHOT\fuxi-plus-shopmall-api-1.0.1-20220112.075826-28.jar;D:\maven\repository\com\fuxiplus\fuxi-plus-model-api\1.0.3-SNAPSHOT\fuxi-plus-model-api-1.0.3-20220112.080654-5.jar;D:\maven\repository\com\fuxiplus\fuxi-plus-bgm-editor-annotation\1.0.3-SNAPSHOT\fuxi-plus-bgm-editor-annotation-1.0.3-20220112.080643-4.jar;D:\maven\repository\com\fuxiplus\fuxi-plus-brand-api\1.0.0-SNAPSHOT\fuxi-plus-brand-api-1.0.0-20220111.090602-15.jar;D:\maven\repository\com\fuxiplus\fuxi-plus-common-core\1.0.2\fuxi-plus-common-core-1.0.2.jar;D:\maven\repository\com\fuxiplus\fuxi-plus-common-swagger\1.0.1\fuxi-plus-common-swagger-1.0.1.jar;D:\maven\repository\org\springframework\cloud\spring-cloud-starter-openfeign\2.2.2.RELEASE\spring-cloud-starter-openfeign-2.2.2.RELEASE.jar;D:\maven\repository\io\github\openfeign\feign-core\10.7.4\feign-core-10.7.4.jar;D:\maven\repository\io\github\openfeign\feign-slf4j\10.7.4\feign-slf4j-10.7.4.jar;D:\maven\repository\io\github\openfeign\feign-hystrix\10.7.4\feign-hystrix-10.7.4.jar;E:\software\IntelliJ IDEA 2017.2.5\plugins\junit\lib\junit-platform-launcher-1.0.0.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 @w@C:\Users\SW00550\AppData\Local\Temp\idea_working_dirs_junit.tmp @C:\Users\SW00550\AppData\Local\Temp\idea_junit.tmp -socket62216
19:45:35.563 [main] INFO com.collect.server.service.test2 - i:0, i >>> 1:0
19:45:35.577 [main] INFO com.collect.server.service.test2 - i:1, i >>> 1:0
19:45:35.577 [main] INFO com.collect.server.service.test2 - i:2, i >>> 1:1
19:45:35.577 [main] INFO com.collect.server.service.test2 - i:3, i >>> 1:1
19:45:35.577 [main] INFO com.collect.server.service.test2 - i:4, i >>> 1:2
19:45:35.577 [main] INFO com.collect.server.service.test2 - i:5, i >>> 1:2
19:45:35.577 [main] INFO com.collect.server.service.test2 - i:6, i >>> 1:3
19:45:35.577 [main] INFO com.collect.server.service.test2 - i:7, i >>> 1:3
19:45:35.587 [main] INFO com.collect.server.service.test2 - i:8, i >>> 1:4
19:45:35.587 [main] INFO com.collect.server.service.test2 - i:9, i >>> 1:4
19:45:35.599 [main] INFO com.collect.server.service.test2 - map:{0=0, 1=0, 2=1, 3=1, 4=2, 5=2, 6=3, 7=3, 8=4, 9=4}
key = 0, value = 0
key = 1, value = 0
key = 2, value = 1
key = 3, value = 1
key = 4, value = 2
key = 5, value = 2
key = 6, value = 3
key = 7, value = 3
key = 8, value = 4
key = 9, value = 4
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
张**
张*1
Process finished with exit code 0

延迟方法:支持链式调用;终结方法:count和forEach方法

   @Slf4j
public class test2 {
    /**
     * 延迟方法:支持链式调用
     * 终结方法:count和forEach方法
     */
    @Test
    public void test2() {
        Stream<String> stream = Stream.of("zhansan", "lisi", "zhaoliu");
        stream.forEach(name -> System.out.println(name));
        /**
         * output
         * zhansan
         lisi
         zhaoliu
         */
    }
    @Test
    public void test3() {
        Stream<String> stream = Stream.of("zhansan", "lisi", "zhaoliu");
        Stream<String> stream2 = stream.filter((String name) -> {return name.startsWith("zhan");});
        stream2.forEach(name -> System.out.println(name));
        /**
         * output
         * zhansan
         */
    }
    @Test
    public void test4() {
        Stream<String> stream = Stream.of("1", "2", "3", "4");
        Stream<Integer> stream2 = stream.map((String s) -> {
            return Integer.parseInt(s);
        });
        stream2.forEach(i -> System.out.print(i + " "));
        /**
         * output:
         * 1 2 3 4
         */
    }
    @Test
    public void test5() {
        Stream<String> stream = Stream.of("1", "2", "3", "22");
        //count
        long count = stream.count();
        System.out.println(count);
        /**
         * output:4
         */
    }
    @Test
    public void test6() {
        String[] arr = {"djkklsdfjklf", "dfsjkksdf", "djksfksdjkfk", "dfsdf", "sfsdfsd", "d", "dd"};
        Stream<String> stream = Stream.of(arr);
        //只要前三个元素
        Stream<String> stream2 = stream.limit(3);
        stream2.forEach(name -> System.out.println(name));
        /**
         * output
         * djkklsdfjklf
         dfsjkksdf
         djksfksdjkfk
         */
    }
    @Test
    public void test7() {
        String[] arr = {"djkklsdfjklf", "dfsjkksdf", "djksfksdjkfk", "dfsdf", "sfsdfsd", "d", "dd"};
        Stream<String> stream = Stream.of(arr);
        //只要跳过三个元素
        Stream<String> stream2 = stream.skip(3);
        stream2.forEach(name -> System.out.println(name));
        /**
         * output
         * dfsdf
         sfsdfsd
         d
         dd
         */
    }
    @Test
    public void test8() {
        Stream<String> stream1 = Stream.of("dfssdfsdf", "33333", "3354555");
        String[] arr = {"djkklsdfjklf", "dfsjkksdf", "djksfksdjkfk", "dfsdf", "sfsdfsd", "d", "dd"};
        Stream<String> stream = Stream.of(arr);
        //合并
        Stream<String> concat = Stream.concat(stream1, stream);
        concat.forEach(name -> System.out.println(name));
        /**
         * output
         * dfssdfsdf
         33333
         3354555
         djkklsdfjklf
         dfsjkksdf
         djksfksdjkfk
         dfsdf
         sfsdfsd
         d
         dd
         */
    }
}

集合元素处理实例

public class test2 {
    @Test
    public void test8() {
        ArrayList<String> two = new ArrayList<>();
        two.add("ddf");
        two.add("ddfg");
        two.add("djkkdfjk");
        two.add("ddf");
        two.add("ddfg");
        two.add("djkkdfjk");
        Stream<String> stream1 = two.stream().filter(name -> name.startsWith("dd")).skip(2);
        Stream<String> stream2 = two.stream().filter(name -> name.length() == 3).limit(3);
        Stream.concat(stream1, stream2).map(name -> new Person(name)).forEach(p ->    System.out.println(p.toString()));
    }
    class Person {
        private String name;
        public Person(String name) {
            this.name = name;
        }
    }
}

stream倒序

records = records.stream().sorted(Comparator.comparing(SurveyShopPageResp::getGmtCreate)
                    .reversed()).collect(Collectors.toList());
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
1月前
|
Java
让星星⭐月亮告诉你,jdk1.8 Java函数式编程示例:Lambda函数/方法引用/4种内建函数式接口(功能性-/消费型/供给型/断言型)
本示例展示了Java中函数式接口的使用,包括自定义和内置的函数式接口。通过方法引用,实现对字符串操作如转换大写、数值转换等,并演示了Function、Consumer、Supplier及Predicate四种主要内置函数式接口的应用。
27 1
|
2月前
|
容器
jdk8新特性-详情查看文档
jdk8新特性-详情查看文档
46 3
|
1月前
|
存储 安全 Java
JDK1.8 新的特性
JDK1.8 新的特性
19 0
|
2月前
|
编解码 安全 Java
jdk8新特性-接口和日期处理
jdk8新特性-接口和日期处理
|
3月前
|
API
JDK8的stream有求和方法吗?
【8月更文挑战第20天】JDK8的stream有求和方法吗?
134 3
|
3月前
|
Java
JDK序列化原理问题之Hessian框架不支持writeObject/readObject方法如何解决
JDK序列化原理问题之Hessian框架不支持writeObject/readObject方法如何解决
|
3月前
|
Oracle Java 关系型数据库
JDK8到JDK29版本升级的新特性问题之未来JDK的升级是否会成为必然趋势,如何理解
JDK8到JDK29版本升级的新特性问题之未来JDK的升级是否会成为必然趋势,如何理解
|
3月前
|
Oracle 安全 Java
JDK8到JDK28版本升级的新特性问题之在Java 15及以后的版本中,密封类和密封接口是怎么工作的
JDK8到JDK28版本升级的新特性问题之在Java 15及以后的版本中,密封类和密封接口是怎么工作的
|
2月前
|
Java 编译器 API
JDK8新特性--lambda表达式
JDK8的Lambda表达式是Java语言的一大进步。它为Java程序提供了更多的编程方式,让代码更加简洁,也让函数式编程的概念在Java中得到了体现。Lambda表达式与Java 8的其他新特性,如Stream API、新的日期时间API一起,极大地提高了Java编程的效率和乐趣。随着时间的流逝,Java开发者对这些特性的理解和应用将会越来越深入,进一步推动Java语言和应用程序的发展。
14 0
|
3月前
|
算法 Java iOS开发
JDK8到JDK27版本升级的新特性问题之JDK 17中G1在资源占用方面有何变化
JDK8到JDK27版本升级的新特性问题之JDK 17中G1在资源占用方面有何变化