java8之Lambda表达式 4:MapReduce开发案例
2015-04-30 17:16
465 查看
简介
通过Lambda中的Stream接口实现MapReduce工具,简单理解就类似于sql之中的分组统计工具,只不过MapReduce是一种可以针对每个数据处理+集合的最终统计操作。具体内容
集合不管怎么改变,它一定是一个动态数组,所以整个MapReduce操作都围绕着对象完成。范例:定义一个购物车类,在集合类里面会保存有多个Car类的对象
public class Car { private String pname; private Integer amouter; private Double price; public Car(String pname, Integer amouter, Double price) { super(); this.pname = pname; this.amouter = amouter; this.price = price; } public Car() { super(); } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Integer getAmouter() { return amouter; } public void setAmouter(Integer amouter) { this.amouter = amouter; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } }
用Map()来进行数据的分别处理
范例:数据的分别处理public class TestDemo { public static void main(String[] args) { List<Car> all=new ArrayList<Car>(); all.add(new Car("java",200,20.8)); all.add(new Car("ios",200,10.8)); all.add(new Car("c",200,2.8)); all.add(new Car("c++",200,10.8)); all.add(new Car("mongo",200,10.8)); all.add(new Car("android",200,12.8)); all.add(new Car("oracle",20,8.8)); all.stream().map(car->{ System.out.print("书名:"+car.getPname()+" 总价:"); return car.getAmouter()*car.getPrice(); }).forEachOrdered(System.out::println); } }
输出结果如下:
书名:c++ 总价:2160.0
书名:mongo 总价:2160.0
书名:android 总价:2560.0
书名:oracle 总价:176.0
从上面的代码可见,map()方法的功能是针对集合的每个数据进行处理。
用Reduce()将集合中的所有数据变为一个结果
如果使用map()方法进行数据的重新组合,那么reduce()就是将集合中的所有数据变为一个结果,就像SQL中的sum(),avg(),count()函数的功能。reduce()方法:
public final Optional<P_OUT> reduce(BinaryOperator<P_OUT> accumulator)
范例,实现购买商品综合的操作
public class TestDemo { public static void main(String[] args) { List<Car> all=new ArrayList<Car>(); all.add(new Car("java",200,20.8)); all.add(new Car("ios",200,10.8)); all.add(new Car("c",200,2.8)); all.add(new Car("c++",200,10.8)); all.add(new Car("mongo",200,10.8)); all.add(new Car("android",200,12.8)); all.add(new Car("oracle",20,8.8)); double result =all.stream().map(car->{ return car.getAmouter()*car.getPrice(); }).reduce((sum,carPrice)->sum+carPrice).get(); System.out.println("购买总金额"+result); } }
代码运行结果如下:
购买总金额13936.0
Map和Reduce一起操作
如果要进行统计,可能会包含:总和,最大值,最小值,平均值,数量在Stream接口里面提供了相应的操作:
处理double数据:
DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)
处理int操作:
IntStream mapToInt(ToIntFunction<? super T> mapper)
处理long操作:
LongStream mapToLong(ToLongFunction<? super T> mapper)
在每个返回的接口里面提供了如下的统计操作方法:
double数据统计(DoubleStream):public DoubleSummaryStatistics summaryStatistics()
int数据统计(IntStream):public IntSummaryStatistics summaryStatistics()
long数据统计(LongStream):public LongSummaryStatistics summaryStatistics()
这些类里面提供了一些列的getXxx()方法用于统计相关信息。
范例:进行reduce功能实现
public class TestDemo { public static void main(String[] args) { List<Car> all=new ArrayList<Car>(); all.add(new Car("java",200,20.8)); all.add(new Car("ios",200,10.8)); all.add(new Car("c",200,2.8)); all.add(new Car("c++",200,10.8)); all.add(new Car("mongo",200,10.8)); all.add(new Car("android",200,12.8)); all.add(new Car("oracle",20,8.8)); DoubleSummaryStatistics result=all.stream().mapToDouble(myCar->{ return myCar.getAmount()*myCar.getPrice(); }).summaryStatistics(); System.out.println("统计量: "+result.getCount()); System.out.println("最大值: "+result.getMax()); System.out.println("最小值: "+result.getMin()); System.out.println("总和: "+result.getSum()); System.out.println("平均值: "+result.getAverage()); } }
输出值:
统计量: 7
最大值: 4160.0
最小值: 176.0
总和: 13936.0
平均值: 1990.857142857143
整个过程就是Mongodb里面用的MapReduce的分析方法。
相关文章推荐
- java8之Lambda表达式 4:MapReduce开发案例
- (持续更新)Java8.0 Lambda表达式实际应用案例
- Android 开发使用 Java 8 中Lambda 表达式功能
- 关于java中Lambda表达式的使用(粗解)
- Java8学习笔记 — 【Lambda表达式】
- Java 8之Lambda表达式
- Java新特性 lambda表达式
- Java 8 lambda表达式引入详解及实例
- JAVA实用案例之验证码开发
- 在Java中使用Lambda表达式统计词频
- java8 Lambda表达式的10个例子(转)
- java8新特性lambda表达式
- JavaWeb - 软件开发的流程,综合案例
- Java8新特性Lambda表达式
- [Java.web]MVC 案例-开发用户模块(注册)
- Java8之lambda表达式详解
- javaSE_8系列博客——Java语言的特性(三)--类和对象(19)--嵌套类(Lambda 表达式)
- Java8-lambda表达式
- Java 8 lambda表达式
- javaSE_8系列博客——Java语言的特性(三)--类和对象(20)--嵌套类(Lambda 表达式--VS--方法引用)