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;
    }
}

猜你喜欢

转载自www.cnblogs.com/dayu007/p/10598950.html