英文:
java Group by color and Summing all Properties to produce a map
问题
以下是您要翻译的部分:
There is list of widgets i want to group them by color while adding other similar properties and produce a map , something like this .
class Widget {
String colour;
Double legs;
Double arms;
}
List<Widget> widList = new ArrayList();
Widget wid1 = new Widget();
wid1.setColor("Yello");
wid1.setLeg(10);
wid1.setArms(10);
Widget wid2 = new Widget();
wid2.setColor("Yello");
wid2.setLeg(20);
wid2.setArms(30);
Widget wid2 = new Widget();
wid2.setColor("White");
wid2.setLeg(20);
wid2.setArms(30);
widgets.stream().collect(groupingBy(Widget::getColour, AllLegstotal(wid1.legs+wid2.legs) and AllArmstotal(wid1.arms+wid2.arms));
result should be
"Yello" : Legs(10+20) , Arms(10+30)
"White" : Legs(20) , Arms(30)
widgets.stream().collect(groupingBy(Widget::getColour, Collectors.summingDouble(All Legs , All Arms));
英文:
There is list of widgets i want to group them by color while adding other similar properties and produce a map , something like this .
class Widget {
String colour;
Double legs;
Double arms;
}
List<Widget> widList = new ArrayList();
Widget wid1 = new Widget();
wid1.setColor("Yello");
wid1.setLeg(10);
wid1.setArms(10);
Widget wid2 = new Widget();
wid2.setColor("Yello");
wid2.setLeg(20);
wid2.setArms(30);
Widget wid2 = new Widget();
wid2.setColor("White");
wid2.setLeg(20);
wid2.setArms(30);
widgets.stream().collect(groupingBy(Widget::getColour, AllLegstotal(wid1.legs+wid2.legs) and AllArmstotal(wid1.arms+wid2.arms));
result should be
"Yello" : Legs(10+20) , Arms(10+30)
"White" : Legs(20) , Arms(30)
widgets.stream().collect(groupingBy(Widget::getColour, Collectors.summingDouble(All Legs , All Arms));
答案1
得分: 1
你可以使用 toMap
收集器来实现这一点。
toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
返回一个收集器,将元素累积到一个 Map 中,其中键和值是将提供的映射函数应用于输入元素的结果。
Map<String, Widget> result = widgets.stream().collect(
Collectors.toMap(
Widget::getColor, // keyMapper
Function.identity(), // valueMapper
(w1, w2) -> new Widget( // mergeFunction
w1.getColor(),
w1.getLegs() + w2.getLegs(),
w1.getArms() + w2.getArms()
)
)
);
mergeFunction 是一个 BinaryOperator,用于将具有相同键的两个元素的值合并在一起。
上面的示例创建了一个新的 Widget 对象,颜色相同,但腿和手臂的总数是 w1 和 w2 的腿和手臂数量之和。
要打印结果:
result.forEach((key, value) ->
System.out.printf("\"%s\": Legs(%.0f), Arms(%.0f)%n", key, value.getLegs(), value.getArms()));
结果:"White": Legs(20), Arms(30) "Yellow": Legs(30), Arms(40)
英文:
You can achieve this by using the toMap
collector.
> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
> Returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements.
Map<String, Widget> result = widgets.stream().collect(
Collectors.toMap(
Widget::getColor, // keyMapper
Function.identity(), // valueMapper
(w1, w2) -> new Widget( // mergeFunction
w1.getColor(),
w1.getLegs() + w2.getLegs(),
w1.getArms() + w2.getArms()
)
)
);
The mergeFunction is a BinaryOperator that is used to combine the values of two elements with the same key.<br />
The example above creates a new Widget object with the same color, but with the total number of legs and arms calculated as the sum of legs and arms of w1 and w2.
To print out the result:
result.forEach((key, value) ->
System.out.printf("\"%s\": Legs(%.0f), Arms(%.0f)%n", key, value.getLegs(), value.getArms()));
Result: "White": Legs(20), Arms(30)
"Yellow": Legs(30), Arms(40)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论