• stream流的统计demo


    Stream流简单demo,直接获取list集合的相关数据

    /**
     * 
     */
    package lambda;
    
    import java.util.ArrayList;
    import java.util.DoubleSummaryStatistics;
    import java.util.List;
    
    /**
     * @author Administrator
     *
     */
    public class StreamDemo {
        public static void main(String[] args) {
            List<Order> list = new ArrayList<>();
            list.add(new Order("西瓜", 2.8, 100));
            list.add(new Order("苹果", 8.8, 100));
            list.add(new Order("芒果", 12.8, 100));
            list.add(new Order("葡萄", 9.8, 100));
    
            // Stream<Order> stream = list.stream();
            DoubleSummaryStatistics statistics
                = list.stream().mapToDouble(((obj) -> obj.getAmount() * obj.getPrice())).summaryStatistics();
            System.out.println("订单均值:" + statistics.getAverage());
            System.out.println("订单最大值:" + statistics.getMax());
            System.out.println("订单最小值:" + statistics.getMin());
            System.out.println("订单总数:" + statistics.getCount());
            System.out.println("订单总额:" + statistics.getSum());
        }
    }
    
    /**
     * 虚拟订单
     */
    class Order {
        // 订单名称
        private String title;
        // 订单价格
        private Double price;
        // 订单数量
        private int amount;
    
        /**
         * @return the title
         */
        public String getTitle() {
            return title;
        }
    
        /**
         * @param title the title to set
         */
        public void setTitle(String title) {
            this.title = title;
        }
    
        /**
         * @return the price
         */
        public Double getPrice() {
            return price;
        }
    
        /**
         * @param price the price to set
         */
        public void setPrice(Double price) {
            this.price = price;
        }
    
        /**
         * @return the amount
         */
        public int getAmount() {
            return amount;
        }
    
        /**
         * @param amount the amount to set
         */
        public void setAmount(int amount) {
            this.amount = amount;
        }
    
        public Order(String title, Double price, int amount) {
            this.title = title;
            this.price = price;
            this.amount = amount;
        }
    }
  • 相关阅读:
    centos7安装es6.4.0
    将mysql数据同步到ES6.4(全量+增量)
    c#基于supersocket的简单websocket服务端收发消息实现
    c#log4net简单好用的配置
    MongoDB安装配置教程
    IntelliJ IDEA 中创建maven项目
    VMware Workstation 的安装和使用
    Redis使用场景
    Redis 下载安装
    MySQL--启动和关闭MySQL服务
  • 原文地址:https://www.cnblogs.com/dayu007/p/10598950.html
Copyright © 2020-2023  润新知