英文:
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<Obj> objList = [obj1, obj2]
obj1.val = "val1"
obj2.val = "val2"
And I want to concat String parameters from that objects with "." separator.
I tried to concatenate them with stream :
objList.stream().collect(Collectors.joining("."));
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("."));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论