zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    一组sharepoint中组合各种功能的JavaScript
    两个完全一样的listview,将第一个中的全部数据复制到第二个中去
    一些测试网站性能的在线免费工具
    C#(WIN FORM)两个窗体间LISTVIEW值的修改
    好的书
    a*寻路算法
    Web网站的性能测试工具
    解决网站中加载js代码速度慢的问题
    SqlDataAdapter和SqlCommand
    Asp.net 备份和还原SQL Server及压缩Access数据库
  • 原文地址:https://www.cnblogs.com/dayu007/p/10598950.html
Copyright © 2011-2022 走看看