Java8Stream流2

上期对stream流大致总结了一下,后面又做了一些练习,大家可以参考一下。

  • 首先需要建一个 Product的实体类,后面会用到
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
    private Long id;
    private Integer num;
    private BigDecimal price;
    private String name;
    private String category;

}
  • 下面是一些具体的案例
Product prod1 = new Product(1L, 1, new BigDecimal("15.5"), "面包", "零食");
    Product prod2 = new Product(2L, 2, new BigDecimal("20"), "饼干", "零食");
    Product prod3 = new Product(3L, 3, new BigDecimal("30"), "月饼", "零食");
    Product prod4 = new Product(4L, 3, new BigDecimal("10"), "青岛啤酒", "啤酒");
    Product prod5 = new Product(5L, 10, new BigDecimal("15"), "百威啤酒", "啤酒");
    List<product> proList = Lists.newArrayList(prod1,prod2,prod3,prod4,prod5);
    @Test
    public void testG(){
        Map<string, list<product>> proMap = proList.stream().collect(Collectors.groupingBy(Product::getCategory));
        for(Map.Entry<string,list<product>> entry : proMap.entrySet()){
            System.out.println("key" + entry.getKey() + " value" + entry.getValue());
        }
    }

    @Test
    public void testCollect1(){
        //&#x6C42;&#x603B;&#x6570;
        Long sum = proList.stream().collect(Collectors.counting());
        //&#x6C42;&#x5E73;&#x5747;&#x6570;&#x91CF;
        Double averageNum = proList.stream().collect(Collectors.averagingInt(Product::getNum));
        //&#x6C42;&#x6700;&#x9AD8;&#x4EF7;&#x683C;
        Optional<bigdecimal> max = proList.stream()
                .map(Product::getPrice)
                .collect(Collectors.maxBy(BigDecimal::compareTo));
        //&#x6C42;&#x6570;&#x91CF;&#x4E4B;&#x548C;
        Long collect = proList.stream().collect(Collectors.summingLong(Product::getNum));
        //&#x4E00;&#x6B21;&#x6027;&#x7EDF;&#x8BA1;&#x6240;&#x6709;&#x4FE1;&#x606F;
        LongSummaryStatistics proLSS = proList.stream().collect(Collectors.summarizingLong(Product::getNum));

        log.info("&#x6C42;&#x603B;&#x6570;:{}", sum);
        log.info("&#x6C42;&#x603B;&#x6C42;&#x5E73;&#x5747;&#x6570;&#x91CF;&#x6570;:{}" ,averageNum);
        log.info("&#x6C42;&#x6700;&#x9AD8;&#x4EF7;&#x683C;:{}" ,max.get());
        log.info("&#x6C42;&#x6570;&#x91CF;&#x4E4B;&#x548C;:{}" ,collect);
        //System.out.println("&#x6C42;&#x603B;&#x6570;" + sum);
        //System.out.println("&#x6C42;&#x5E73;&#x5747;&#x6570;&#x91CF;" + averageNum);
//        System.out.println("&#x6C42;&#x6700;&#x9AD8;&#x4EF7;&#x683C;" + max);
//        System.out.println("&#x6C42;&#x6570;&#x91CF;&#x4E4B;&#x548C;" + collect);
        System.out.println("&#x4E00;&#x6B21;&#x6027;&#x7EDF;&#x8BA1;&#x6240;&#x6709;&#x4FE1;&#x606F;" + proLSS);

        //&#x5C06;&#x4EA7;&#x54C1;&#x6309;&#x6570;&#x91CF;&#x662F;&#x5426;&#x5927;&#x4E8E;5&#x5206;&#x533A;
        Map<boolean, list<product>> partList = proList.stream().collect(Collectors.partitioningBy(n -> n.getNum() > 5));
        log.info("&#x6309;&#x6570;&#x91CF;&#x662F;&#x5426;&#x5927;&#x4E8E;5&#x5206;&#x533A;:{}",partList);
        //&#x5C06;&#x4EA7;&#x54C1;&#x6309;&#x7C7B;&#x522B;&#x5206;&#x7EC4;
        Map<string, list<product>> groupList = proList.stream().collect(Collectors.groupingBy(p -> p.getCategory()));
        Map<string, list<product>> groupList1 = proList.stream().collect(Collectors.groupingBy(Product::getCategory));
        log.info("&#x5C06;&#x4EA7;&#x54C1;&#x6309;&#x7C7B;&#x522B;&#x5206;&#x7EC4;:{}",groupList);
        log.info("&#x5C06;&#x4EA7;&#x54C1;&#x6309;&#x7C7B;&#x522B;&#x5206;&#x7EC4;:{}",groupList1);

    }

    @Test
    public void testJoiningReducing(){
        //&#x5C06;&#x6240;&#x6709;&#x4EA7;&#x54C1;&#x7684; &#x540D;&#x79F0; &#x62FC;&#x63A5;&#x6210;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;
        String names = proList.stream().map(Product::getName).collect(Collectors.joining("_"));
        log.info("&#x6240;&#x6709;&#x4EA7;&#x54C1;&#x540D;:{}",names);

        List<string> strList = Arrays.asList("A", "B", "C", "D");
        String strJoining = strList.stream().collect(Collectors.joining("+_+"));
        log.info("&#x5B57;&#x7B26;&#x4E32;:{}",strJoining);

        Integer collect = proList.stream().collect(Collectors.reducing(0, Product::getNum, (x, y) -> (x + y + 1)));
        log.info("collect:{}",collect);

        //stream&#x4E2D;&#x7684;reduce
        Optional<integer> reduce = proList.stream().map(Product::getNum).reduce(Integer::compare);
        log.info("stream->reduce:{}",reduce.get());
    }

    @Test
    public void testSorted1(){
        //&#x6309; &#x6570;&#x91CF;&#x5347;&#x5E8F;&#x6392;&#x5217;
        List<product> sortedList = proList.stream().sorted(Comparator.comparing(Product::getNum))
                .collect(Collectors.toList());
        log.info("sorted:{}",sortedList);

        //&#x6309;&#x6570;&#x91CF;&#x5012;&#x53D9;&#x6392;
        List<product> sortedListReversed = proList.stream().sorted(Comparator.comparing(Product::getNum).reversed())
                .collect(Collectors.toList());
        log.info("sored.reversed:{}",sortedListReversed);

        //&#x5148;&#x6309;&#x6570;&#x91CF;&#x6392;&#xFF0C;&#x518D;&#x6309;&#x4EF7;&#x683C;&#x6392; (&#x9ED8;&#x8BA4;&#x5347;&#x5E8F;&#xFF09;
        List<product> collect = proList.stream().sorted(Comparator.comparing(Product::getNum).thenComparing(Product::getPrice))
                .collect(Collectors.toList());
        log.info("&#x5148;&#x6309;&#x6570;&#x91CF;&#x6392;&#xFF0C;&#x518D;&#x6309;&#x4EF7;&#x683C;&#x6392; :{}",collect);

        //&#x5148;&#x6309;&#x6570;&#x91CF;&#x6392;&#xFF0C;&#x518D;&#x6309;&#x4EF7;&#x683C;&#x6392; (&#x964D;&#x5E8F;&#x6392;&#xFF09;
        List<product> collect1 = proList.stream()
                .sorted(Comparator.comparing(Product::getNum).thenComparing(Product::getPrice).reversed())
                .collect(Collectors.toList());
        log.info("&#x5148;&#x6309;&#x6570;&#x91CF;&#x6392;&#xFF0C;&#x518D;&#x6309;&#x4EF7;&#x683C;&#x964D;&#x5E8F;&#x6392; :{}",collect1);

        //&#x5148;&#x6309;&#x6570;&#x91CF;&#x6392; &#x518D;&#x6309;&#x4EF7;&#x683C; &#x81EA;&#x5B9A;&#x4E49;&#x6392;
        List<product> collect2 = proList.stream().sorted((p1, p2) -> {
            if (p1.getNum() == p2.getNum()){
                return p1.getPrice().compareTo(p2.getPrice()) ;
            }else {
                return p2.getNum() - p1.getNum();
            }
        }).collect(Collectors.toList());
        log.info("&#x5148;&#x6309;&#x6570;&#x91CF;&#x6392; &#x518D;&#x6309;&#x4EF7;&#x683C; &#x81EA;&#x5B9A;&#x4E49;&#x6392;:{}",collect2);
    }

    //&#x6D4B;&#x8BD5;stream&#x4E2D;reduce (&#x89C4;&#x7EA6; &#x4E5F;&#x6210;&#x4E3A;&#x7F29;&#x51CF; &#x662F;&#x628A;&#x4E00;&#x4E2A;&#x6D41;&#x7F29;&#x51CF;&#x6210;&#x4E00;&#x4E2A;&#x503C; &#x80FD;&#x5B9E;&#x73B0;&#x5BF9;&#x96C6;&#x5408;&#x6C42;&#x548C;&#xFF0C;&#x6C42;&#x79EF; &#x548C;&#x6C42;&#x6700;&#x503C;&#x7684;&#x64CD;&#x4F5C;)
    @Test
    public void testStreamReduce() {
        //&#x6C42;&#x5546;&#x54C1; &#x4EF7;&#x683C;&#x7684;&#x603B;&#x548C; &#x65B9;&#x5F0F;1
        Optional<bigdecimal> sum = proList.stream().map(Product::getPrice)
                .reduce((x, y) -> x.add(y));
        log.info("&#x6C42;&#x548C;:{}",sum.get());

        //&#x6C42;&#x5546;&#x54C1; &#x6570;&#x91CF;&#x7684;&#x603B;&#x548C; &#x65B9;&#x5F0F;2
        Optional<integer> sum1 = proList.stream().map(Product::getNum)
                .reduce(Integer::sum);
        log.info("&#x6C42;&#x548C;:{}",sum1.get());

        //&#x6C42;&#x5546;&#x54C1; &#x4EF7;&#x683C;&#x7684;&#x603B;&#x548C; &#x65B9;&#x5F0F;2
        Optional<bigdecimal> sum2 = proList.stream().map(Product::getPrice)
                .reduce(BigDecimal::add);
        log.info("&#x6C42;&#x548C;sum2:{}",sum2.get());

        //&#x6C42;&#x5546;&#x54C1; &#x4EF7;&#x683C;&#x7684;&#x603B;&#x548C; &#x65B9;&#x5F0F;3
        BigDecimal sum3 = proList.stream().map(Product::getPrice)
                .reduce(new BigDecimal("0"),BigDecimal::add);
        log.info("&#x6C42;&#x548C;sum3:{}",sum3);

        //&#x6C42;&#x4E58;&#x79EF;
        Optional<integer> product = proList.stream().map(Product::getNum).reduce((x, y) -> x * y);
        log.info("&#x4E58;&#x79EF;:{}",product);

        //&#x6C42;&#x6700;&#x5927;&#x503C; &#x65B9;&#x5F0F; 1
        Optional<bigdecimal> maxPrice = proList.stream().map(Product::getPrice)
                .reduce((x, y) -> x.compareTo(y) == 1 ? x : y);
        log.info("&#x5546;&#x54C1;&#x4EF7;&#x683C;&#x7684;&#x6700;&#x5927;&#x503C;:{}",maxPrice);

        //&#x6C42;&#x6700;&#x5927;&#x503C; &#x65B9;&#x5F0F;2
        BigDecimal max2 = proList.stream().map(Product::getPrice)
                .reduce(new BigDecimal(1), BigDecimal::max);
        log.info("&#x6C42; &#x6700;&#x5927;&#x503C; &#x65B9;&#x5F0F;2:{}",max2);
    }
</bigdecimal></integer></bigdecimal></integer></bigdecimal></product></product></product></product></product></integer></string></string,></string,></boolean,></bigdecimal></string,list<product></string,></product>

Original: https://www.cnblogs.com/huoyl/p/Java8Stream2.html
Author: h*z
Title: Java8Stream流2

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/598584/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球