java8之Lambda表示式 4:MapReduce開發案例
簡介
通過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的分析方法。