Group by颜色并将所有属性相加以生成一个映射。

huangapple go评论61阅读模式
英文:

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) -&gt;
            System.out.printf(&quot;\&quot;%s\&quot;: Legs(%.0f), Arms(%.0f)%n&quot;, key, value.getLegs(), value.getArms()));

Result: &quot;White&quot;: Legs(20), Arms(30)
&quot;Yellow&quot;: Legs(30), Arms(40)

huangapple
  • 本文由 发表于 2023年3月1日 13:21:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定