您的位置:首页 > 编程语言 > Java开发

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的分析方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: