英文:
Create arraylist from list of maps for certain key value with java 8
问题
以下是翻译好的内容:
我有一个包含映射列表的对象。每个映射如下所示:
{
id: "something",
link: "someLink"
}
我想要创建一个包含所有映射中 id 的数组列表。我可以使用一个简单的循环来实现:
List<String> Ids = new ArrayList<>();
List<Map<String, String>> maps = pojo.getMaps();
for (Map<String, String> map : maps) {
Ids.add(map.get("id"));
}
但是如何在Java 8的流式处理中用一行代码完成呢?我从未在映射上使用过它,所以感到困惑。
我猜大致的思路是这样的,但实际上我不太确定:
List<String> ids = pojo.getMaps().stream().map(map -> map.get("id")).collect(Collectors.toList());
英文:
I have an object with a list of maps. Each map looks like this:
{
id: "something",
link: "someLink"
}
I am trying to create an array list of all the id's in the maps. I can do this with a simple loop
List<String> Ids = new ArrayList<>();
List<Map<String, String>> maps = pojo.getMaps();
for(Map<String, String> map: maps) {
Ids.add(map.get("id"));
}
But how is this done in one line using java 8 streams? I have never used it with maps so I am lost.
I assume it would be along the lines of something like this but i honestly dont know
List<String> ids = pojo.getMaps().stream().map(Map.Entry:: ???? ).collect(Collectors.toList())
答案1
得分: 4
> `pojo.getMaps().stream()`
到目前为止,一切都很好。您现在有一个 `Map<String, String>` 对象的流。我们只需要从中获取键。因此,对于你奇怪的地图对象之一,我们如何将其转化为键值呢?
看起来一个简单的 `map.get("id")` 就可以完成这个工作,是吗?
所以让我们将其放入lambda表达式中:
`pojo.getMaps().stream().map(theMap -> theMap.get("id"))`
现在我们有一个带有id的 `Stream<String>`。
## 但是,重要的一点!
您一开始就拥有一个看起来像对象的映射对象,这是一个巨大的代码异味。很可能你应该回到项目的前几步来修复这个问题。您确实应该有一个表示此链接概念的类:
@Value
public class Link {
String id, link;
}
然后从这里开始:
listOfLinks.stream().map(Link::getId).distinct().collect(...);
注意:上面的示例使用了[Lombok的`@Value`](https://projectlombok.org/features/Value)。
## 函数式只是一种工具
请注意,没有必要重新编写您的代码以“使用java 8功能”。比较:
var ids = pojo.getMaps().stream()
.map(m -> m.get("id"))
.collect(Collectors.toList());
与:
var ids = new ArrayList<String>();
for (var m : pojo.getMaps()) ids.add(m.get("id"));
两者的代码量几乎相同。
英文:
> pojo.getMaps().stream()
So far, so good. You now have a stream of Map<String, String>
objects. We need to just get the keys from this. So, given one of your weird map things, how do we turn that into the key value?
Looks like a trivial map.get("id")
does that job, no?
So let's put that in the lambda:
pojo.getMaps().stream().map(theMap -> theMap.get("id"))
and now we have a Stream<String>
with ids.
HOWEVER, big note!
The fact that you start out with a map object that looks like an object is a giant code smell. Most likely you should go back a few steps in your project and fix that instead. You really ought to have a class that represents this link concept:
@Value
public class Link {
String id, link;
}
and go from there:
listOfLinks.stream().map(Link::getId).distinct().collect(...);
NB: The above uses Lombok's @Value
.
functional is just a tool
Note that there is no need to rewrite your code to 'use java 8 features'. Compare:
var ids = pojo.getMaps().stream()
.map(m -> m.get("id"))
.collect(Collectors.toList());
with:
var ids = new ArrayList<String>();
for (var m : pojo.getMaps()) ids.add(m.get("id"));
it's about the same amount of code.
答案2
得分: 1
你可以在循环中以相同的方式从映射中获取 id
的值。这里使用 .map()
来将 Map<String, String>
转换为 String
,即将映射转换为键 id
的值。
List<String> ids = pojo.getMaps().stream()
.map(e -> e.get("id"))
.collect(Collectors.toList());
英文:
You can use get the value of id
from map same way in loop. Here .map()
used to transform Map<String, String>
to String
means transform Map to value of key id
List<String> ids = pojo.getMaps().stream() // Stream<Map<String, String>>
.map(e -> e.get("id")) // Stream<String>
.collect(Collectors.toList());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论