我试图在java Stream中实现map-reduce范例这个1命令。 例如,我有各种客户的订单列表,每个订单都有订单状态,客户ID和订单金额:
<style>
.address{
position: absolute;
left: 234px;
bottom: 160px;
width: 110px;
height: 115px;
}
</style>
<script>
$('.address').click(function(){
for(var i=0;i<=$('.address').length;i++){
switch (i) {
case 0 :
$('.embed-container, .maps').html('<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d945.7084267048235!2d73.8854388291583!3d18.536415399212462!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xdda3387f821de029!2sWink+%26+Nod!5e0!3m2!1sen!2sin!4v1525613363322" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>');
break;
case 1 :
$('.embed-container, .maps').html('<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3784.450403270009!2d73.81459871445705!3d18.463246687442403!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3bc2956a20fbac17%3A0x91987f7033fd2e3c!2sAbhiruchi+Mall+and+Multiplex!5e0!3m2!1sen!2sin!4v1525612885113" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>');
break;
default:
$('.embed-container, .maps').html(' <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d945.7084267048235!2d73.8854388291583!3d18.536415399212462!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xdda3387f821de029!2sWink+%26+Nod!5e0!3m2!1sen!2sin!4v1525613363322" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>');
break;
}
}
});
</script>
我想得到每个客户的金额总和。
做这样的事情,并不是由客户对总和进行分组,而是计算所有客户的总和:
import React, { Component } from "react";
import { render } from "react-dom";
class CView extends Component {
someFunc() {
alert(1);
}
render() {
return <div>Hello, there</div>;
}
}
class App extends Component {
getControl() {
this.cv = <CView />;
return this.cv;
}
render() {
return (
<div>
<h2 onClick={() => this.cv.someFunc()}>Click Me</h2>
{this.getControl()}
</div>
);
}
}
render(<App />, document.getElementById("root"));
有人有任何想法吗?
答案 0 :(得分:4)
为了为每个Customer
生成单独的总计,您需要按客户ID对Customer
进行分组(使用collect(Collectors.groupingBy())
)。然后,您可以使用Collectors.summingInt()
对每组Customer
的数量进行求和:
Map<String,Integer> totals =
customers.stream()
.filter(a->a.status.equals("A"))
.collect(Collectors.groupingBy(Customer::getID,
Collectors.summingInt(Customer::getAmount)));
这将返回以下Map
:
{cust1=750, cust2=1568, cust3=845, cust4=86}