zoukankan      html  css  js  c++  java
  • 高效 告别996,开启java高效编程之门 3-9实战:常用中间操作演示之:去重/跳过/截断

    1    重点

    去重,跳过,截断

    动态分页

    实战演示之 取商品的名称和总价打印:(stream流转化成list的方法)(打印两个属性)

    2    实战演示之去重(distinct):
        @Test
        /**
         * distinct 去重
         * 打印Sku的目录分类
         */
        public void distinctTest(){
            list.stream()
                    //注意map和distinct的使用
                    .map(Sku::getSkuCategory)
                    .distinct()
                    .forEach(item->System.out.println(JSONObject.toJSONString(item,true)));
        }

    打印日志:

    "ELECTRONICS"
    "CLOTHING"
    "BOOKS"
    "SPROTS"
    
    Process finished with exit code 0
    3    实战演示之跳过(skip):
    /**
         * skip跳过
         * 按照总价排序,跳过前三条
         * reversed 不能放在Sku->Sku.getTotalPrice()之后,原因暂时不明
         */
        @Test
        public void skipTest(){
            list.stream()
                    .sorted(Comparator.comparing(Sku::getTotalPrice).reversed())
                    .skip(4)
                    .forEach(item->System.out.println(JSONObject.toJSONString(item,true)));
        }

    打印日志:

    D:javajdkjdk9jdk-9+181_windows-x64_rijava-se-9-rijdk-9injava.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4libidea_rt.jar=13650:D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4in" -Dfile.encoding=UTF-8 -classpath "D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4libidea_rt.jar;D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4pluginsjunitlibjunit-rt.jar;D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4pluginsjunitlibjunit5-rt.jar;F:xiangmu3Xin	est996	arget	est-classes;F:xiangmu3Xin	est996	argetclasses;F:xiangmu3Xin	est996libcomgoogleguavaguava28.2-jreguava-28.2-jre.jar;F:xiangmu3Xin	est996libcomgoogleguavafailureaccess1.0.1failureaccess-1.0.1.jar;F:xiangmu3Xin	est996libcomgoogleguavalistenablefuture9999.0-empty-to-avoid-conflict-with-guavalistenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;F:xiangmu3Xin	est996libcomgooglecodefindbugsjsr3053.0.2jsr305-3.0.2.jar;F:xiangmu3Xin	est996liborgcheckerframeworkchecker-qual2.10.0checker-qual-2.10.0.jar;F:xiangmu3Xin	est996libcomgoogleerrorproneerror_prone_annotations2.3.4error_prone_annotations-2.3.4.jar;F:xiangmu3Xin	est996libcomgooglej2objcj2objc-annotations1.3j2objc-annotations-1.3.jar;F:xiangmu3Xin	est996libjunitjunit4.12junit-4.12.jar;F:xiangmu3Xin	est996liborghamcresthamcrest-core1.3hamcrest-core-1.3.jar;F:xiangmu3Xin	est996libcomalibabafastjson1.2.58fastjson-1.2.58.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.imooc.zhangxiaoxi.stream.StreamOperator,skipTest
    {
        "skuCategory":"BOOKS",
        "skuId":2020005,
        "skuName":"剑指高效编程",
        "skuPrice":288.0,
        "totalNum":1,
        "totalPrice":288.0
    }
    {
        "skuCategory":"CLOTHING",
        "skuId":2020002,
        "skuName":"T-shirt",
        "skuPrice":50.0,
        "totalNum":2,
        "totalPrice":100.0
    }
    {
        "skuCategory":"BOOKS",
        "skuId":2020003,
        "skuName":"人生的枷锁",
        "skuPrice":30.0,
        "totalNum":1,
        "totalPrice":30.0
    }
    {
        "skuCategory":"BOOKS",
        "skuId":2020004,
        "skuName":"老人与海",
        "skuPrice":20.0,
        "totalNum":1,
        "totalPrice":20.0
    }
    
    Process finished with exit code 0
    4    实战演示之截断(limit):
    /**
         * limit(n)截断前n条
         * 总价从大到小排序,截取前4条
         */
        @Test
        public void limitTest(){
            list.stream()
                    .sorted(Comparator.comparing(Sku::getTotalPrice).reversed())
                    .limit(4)
                    .forEach(item->System.out.println(JSONObject.toJSONString(item,true)));
        }

    打印日志:

    {
        "skuCategory":"ELECTRONICS",
        "skuId":2020008,
        "skuName":"ThinkPad",
        "skuPrice":5000.0,
        "totalNum":1,
        "totalPrice":5000.0
    }
    {
        "skuCategory":"SPROTS",
        "skuId":2020007,
        "skuName":"杠铃",
        "skuPrice":2000.0,
        "totalNum":1,
        "totalPrice":2000.0
    }
    {
        "skuCategory":"ELECTRONICS",
        "skuId":2020001,
        "skuName":"无人机",
        "skuPrice":999.0,
        "totalNum":1,
        "totalPrice":999.0
    }
    {
        "skuCategory":"CLOTHING",
        "skuId":2020006,
        "skuName":"大头皮鞋",
        "skuPrice":300.0,
        "totalNum":1,
        "totalPrice":300.0
    }
    
    Process finished with exit code 0
    5    实战演示之 动态分页:
       /**
         * 动态分页
         * 每页展示5条,第3也
         */
        @Test
        public void paging(){
            list.stream()
                    .skip((1-1)*5)
                    .limit(5)
                    .forEach(item->System.out.println(JSONObject.toJSONString(item,true)));
        }

     打印日志:

    {
        "skuCategory":"ELECTRONICS",
        "skuId":2020001,
        "skuName":"无人机",
        "skuPrice":999.0,
        "totalNum":1,
        "totalPrice":999.0
    }
    {
        "skuCategory":"CLOTHING",
        "skuId":2020002,
        "skuName":"T-shirt",
        "skuPrice":50.0,
        "totalNum":2,
        "totalPrice":100.0
    }
    {
        "skuCategory":"BOOKS",
        "skuId":2020003,
        "skuName":"人生的枷锁",
        "skuPrice":30.0,
        "totalNum":1,
        "totalPrice":30.0
    }
    {
        "skuCategory":"BOOKS",
        "skuId":2020004,
        "skuName":"老人与海",
        "skuPrice":20.0,
        "totalNum":1,
        "totalPrice":20.0
    }
    {
        "skuCategory":"BOOKS",
        "skuId":2020005,
        "skuName":"剑指高效编程",
        "skuPrice":288.0,
        "totalNum":1,
        "totalPrice":288.0
    }
    
    Process finished with exit code 0
    6    实战演示之 取商品的名称和总价打印:
    /**
         * 展示出商品的名称和价格,String,Double
         */
        @Test
        public void printMapTwo(){
    
            Map<String,Double> mMap=new HashMap<String,Double>();
    
            List<Sku> list2 = list.stream().sorted(Comparator.comparing(Sku::getTotalPrice)).collect(Collectors.toList());
    
            for(Sku sku:list2){
                mMap.put(sku.getSkuName(),sku.getTotalPrice());
            }
            System.out.println(JSONObject.toJSONString(mMap,true));
        }

    打印日志:

    {
        "老人与海":20.0,
        "人生的枷锁":30.0,
        "ThinkPad":5000.0,
        "T-shirt":100.0,
        "无人机":999.0,
        "大头皮鞋":300.0,
        "杠铃":2000.0,
        "剑指高效编程":288.0
    }
    
    Process finished with exit code 0
  • 相关阅读:
    Yocto开发笔记之《Tip-bitbake常用命令》(QQ交流群:519230208)
    Yocto开发笔记之《Tip-设置程序开机启动》(QQ交流群:519230208)
    Yocto开发笔记之《Tip-应用程序无法在目标机运行》(QQ交流群:519230208)
    激光雷达技术
    Yocto开发笔记之《网卡配置》(QQ交流群:519230208)
    Yocto开发笔记之《错误记录》(QQ交流群:519230208)
    Yocto开发笔记之《工具使用:TFTP & NFS & SSH》(QQ交流群:519230208)
    Yocto开发笔记之《U-boot启动内核流程》(QQ交流群:519230208)
    自定义选择提示框
    uitextfield输入字符限制
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/12765040.html
Copyright © 2011-2022 走看看