Java Stream MapReduce大数据开发模型

实现一个购买商品后,对数据进行处理统计的功能.

将购买的商品信息保存在Orders类中

public class Orders {
          
   
    private String name;
    private double price;
    private int amount;

    public Orders(String name, double price, int amount) {
          
   
        this.name = name;
        this.price = price;
        this.amount = amount;
    }

    public String getName() {
          
   
        return name;
    }

    public void setName(String name) {
          
   
        this.name = name;
    }

    public double getPrice() {
          
   
        return price;
    }

    public void setPrice(double price) {
          
   
        this.price = price;
    }

    public int getAmount() {
          
   
        return amount;
    }

    public void setAmount(int amount) {
          
   
        this.amount = amount;
    }

通过map()处理数据,输出购买每一件商品的总价

public static void main(String[] args) {
          
   
        List<Orders> list = new ArrayList<>();
        list.add(new Orders("dq ", 60.00, 80));
        list.add(new Orders("dq android", 100.20, 73));
        list.add(new Orders("dq java", 72.02, 15));
        //计算购买每一件商品的总价
        list.stream().map((x) -> x.getAmount() * x.getPrice()).forEach(System.out::println);
    }

通过reduce()对map()处理后的数据进行统计

double all = list.stream().map((x) -> x.getAmount() * x.getPrice()).reduce((x, y) -> x + y).get();
        System.out.println(String.format("%.2f", all));

使用DoubleStream接口中DoubleSummaryStatistics类进行数据统计

DoubleSummaryStatistics statistics=list.stream().mapToDouble((x)->x.getAmount()* x.getPrice()).summaryStatistics();
        System.out.println("商品品种"+statistics.getCount()+"商品总花费"+statistics.getSum()+"商品平均花费"+statistics.getAverage()+"最高花费"+statistics.getMax()+"最低花费"+statistics.getMin());

总代码:

import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;

public class Orders {
          
   
    private String name;
    private double price;
    private int amount;

    public Orders(String name, double price, int amount) {
          
   
        this.name = name;
        this.price = price;
        this.amount = amount;
    }

    public String getName() {
          
   
        return name;
    }

    public void setName(String name) {
          
   
        this.name = name;
    }

    public double getPrice() {
          
   
        return price;
    }

    public void setPrice(double price) {
          
   
        this.price = price;
    }

    public int getAmount() {
          
   
        return amount;
    }

    public void setAmount(int amount) {
          
   
        this.amount = amount;
    }

    public static void main(String[] args) {
          
   
        List<Orders> list = new ArrayList<>();
        list.add(new Orders("dq ", 60.00, 80));
        list.add(new Orders("dq android", 100.20, 73));
        list.add(new Orders("dq java", 72.02, 15));
        DoubleSummaryStatistics statistics=list.stream().mapToDouble((x)->x.getAmount()* x.getPrice()).summaryStatistics();
        System.out.println("商品品种"+statistics.getCount()+"商品总花费"+statistics.getSum()+"商品平均花费"+statistics.getAverage()+"最高花费"+statistics.getMax()+"最低花费"+statistics.getMin());
    }
}

参考<<第一行代码>>第13章

经验分享 程序员 微信小程序 职场和发展