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