从对象列表中合并参数

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

Concat parameters from object list

问题

我有一系列的对象:

List<Obj> objList = [obj1, obj2]

obj1.val = "val1";
obj2.val = "val2";

我想要使用“.”作为分隔符,将这些对象的字符串参数连接起来。我尝试使用流进行连接:

objList.stream().collect(Collectors.joining("."));

但我无法弄清楚如何仅连接对象的参数。

在这种情况下,结果应为“val1.val2”。

英文:

I have list of objects:

List&lt;Obj&gt; objList = [obj1, obj2]

obj1.val = &quot;val1&quot;
obj2.val = &quot;val2&quot;

And I want to concat String parameters from that objects with "." separator.
I tried to concatenate them with stream :

objList.stream().collect(Collectors.joining(&quot;.&quot;));

But I can't figure it out how to concat only parameters from objects.

In this case the result should be "val1.val2"

答案1

得分: 0

你在这里缺少了 map 操作符。你需要在收集之前的流处理流程中添加 map 操作符。下面是示例代码:

objList.stream().map(Obj::val).collect(Collectors.joining("."));
英文:

You are missing the map operator here. You have to add map operator to the stream processing pipeline before collecting it. Here's how it looks.

objList.stream().map(Obj::val).collect(Collectors.joining(&quot;.&quot;));

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

发表评论

匿名网友

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

确定