当前位置:   article > 正文

MapReduce模型(对数据的统计分析)_使用mapreduce统计数量占比

使用mapreduce统计数量占比

1.编写一个简单的数据统计操作

  1. package 集合框架库.Map集合.MapReduce;
  2. public class Order {
  3. private String title;
  4. private double price;
  5. private int amonu;
  6. public Order(String title, double price, int amonu) {
  7. this.title = title;
  8. this.price = price;
  9. this.amonu = amonu;
  10. }
  11. public String getTitle() {
  12. return title;
  13. }
  14. public void setTitle(String title) {
  15. this.title = title;
  16. }
  17. public double getPrice() {
  18. return price;
  19. }
  20. public void setPrice(double price) {
  21. this.price = price;
  22. }
  23. public int getAmonu() {
  24. return amonu;
  25. }
  26. public void setAmonu(int amonu) {
  27. this.amonu = amonu;
  28. }
  29. @Override
  30. public String toString() {
  31. return "Order{" +
  32. "title='" + title + '\'' +
  33. ", price=" + price +
  34. ", amonu=" + amonu +
  35. '}';
  36. }
  37. }

2.测试 

  1. package 集合框架库.Map集合.MapReduce;
  2. import java.util.ArrayList;
  3. import java.util.DoubleSummaryStatistics;
  4. import java.util.List;
  5. public class TestMapReduce {
  6. public static void main(String[] args) {
  7. List<Order> orderList =new ArrayList <> ( );
  8. orderList.add ( new Order ( "Iphone",899.45,22 ) );
  9. orderList.add(new Order("外星人",1897.77,45));
  10. orderList.add ( new Order ( "MacBookPro",1899.96,14 ) );
  11. orderList.add ( new Order ( "Java学习宝典",7888.9,56 ) );
  12. double TotalPrice=orderList.stream ().map ( order -> order.getPrice ()*order.getAmonu ()).reduce ( (sum,x )->sum+x ).get ();
  13. System.out.println ("所花的总数额为:"+TotalPrice );
  14. DoubleSummaryStatistics dss=orderList.stream ().mapToDouble ( order->order.getPrice ()*order.getAmonu () ).summaryStatistics ();
  15. System.out.println ("总量:"+dss.getCount () );
  16. System.out.println ("平均值"+dss.getAverage () );
  17. System.out.println ("最大值:"+dss.getMax () );
  18. System.out.println ("最小值:"+dss.getMin () );
  19. System.out.println ("总和:"+dss.getSum () );
  20. }
  21. }

3.运行结果 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/781102
推荐阅读
相关标签
  

闽ICP备14008679号