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:\java\jdk\jdk9\jdk-9+181_windows-x64_ri\java-se-9-ri\jdk-9\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=13650:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar;D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\plugins\junit\lib\junit-rt.jar;D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\plugins\junit\lib\junit5-rt.jar;F:\xiangmu3\Xin\test996\target\test-classes;F:\xiangmu3\Xin\test996\target\classes;F:\xiangmu3\Xin\test996\lib\com\google\guava\guava\28.2-jre\guava-28.2-jre.jar;F:\xiangmu3\Xin\test996\lib\com\google\guava\failureaccess\1.0.1\failureaccess-1.0.1.jar;F:\xiangmu3\Xin\test996\lib\com\google\guava\listenablefuture\9999.0-empty-to-avoid-conflict-with-guava\listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;F:\xiangmu3\Xin\test996\lib\com\google\code\findbugs\jsr305\3.0.2\jsr305-3.0.2.jar;F:\xiangmu3\Xin\test996\lib\org\checkerframework\checker-qual\2.10.0\checker-qual-2.10.0.jar;F:\xiangmu3\Xin\test996\lib\com\google\errorprone\error_prone_annotations\2.3.4\error_prone_annotations-2.3.4.jar;F:\xiangmu3\Xin\test996\lib\com\google\j2objc\j2objc-annotations\1.3\j2objc-annotations-1.3.jar;F:\xiangmu3\Xin\test996\lib\junit\junit\4.12\junit-4.12.jar;F:\xiangmu3\Xin\test996\lib\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;F:\xiangmu3\Xin\test996\lib\com\alibaba\fastjson\1.2.58\fastjson-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

 

posted on 2020-04-24 06:13  菜鸟乙  阅读(257)  评论(0编辑  收藏  举报