用Java 8从一组地图的列表中按特定键值创建ArrayList。

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

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: &quot;something&quot;,
  link: &quot;someLink&quot;
}

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&lt;String&gt; Ids = new ArrayList&lt;&gt;();
        
 List&lt;Map&lt;String, String&gt;&gt; maps = pojo.getMaps();
        
 for(Map&lt;String, String&gt; map: maps) {
       Ids.add(map.get(&quot;id&quot;));
    }

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&lt;String&gt; ids = pojo.getMaps().stream().map(Map.Entry::  ???? ).collect(Collectors.toList())

答案1

得分: 4

&gt; `pojo.getMaps().stream()`

到目前为止,一切都很好。您现在有一个 `Map&lt;String, String&gt;` 对象的流。我们只需要从中获取键。因此,对于你奇怪的地图对象之一,我们如何将其转化为键值呢?

看起来一个简单的 `map.get(&quot;id&quot;)` 就可以完成这个工作,是吗?

所以让我们将其放入lambda表达式中:

`pojo.getMaps().stream().map(theMap -&gt; theMap.get(&quot;id&quot;))`

现在我们有一个带有id的 `Stream&lt;String&gt;`。


## 但是,重要的一点!

您一开始就拥有一个看起来像对象的映射对象,这是一个巨大的代码异味。很可能你应该回到项目的前几步来修复这个问题。您确实应该有一个表示此链接概念的类:

@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 -&gt; m.get(&quot;id&quot;))
  .collect(Collectors.toList());

与:

var ids = new ArrayList&lt;String&gt;();
for (var m : pojo.getMaps()) ids.add(m.get(&quot;id&quot;));

两者的代码量几乎相同。
英文:

> pojo.getMaps().stream()

So far, so good. You now have a stream of Map&lt;String, String&gt; 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(&quot;id&quot;) does that job, no?

So let's put that in the lambda:

pojo.getMaps().stream().map(theMap -&gt; theMap.get(&quot;id&quot;))

and now we have a Stream&lt;String&gt; 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 -&gt; m.get(&quot;id&quot;))
      .collect(Collectors.toList());

with:

    var ids = new ArrayList&lt;String&gt;();
    for (var m : pojo.getMaps()) ids.add(m.get(&quot;id&quot;));

it's about the same amount of code.

答案2

得分: 1

你可以在循环中以相同的方式从映射中获取 id 的值。这里使用 .map() 来将 Map&lt;String, String&gt; 转换为 String,即将映射转换为键 id 的值。

List&lt;String&gt; ids = pojo.getMaps().stream()
                                 .map(e -&gt; e.get(&quot;id&quot;))
                                 .collect(Collectors.toList());
英文:

You can use get the value of id from map same way in loop. Here .map() used to transform Map&lt;String, String&gt; to String means transform Map to value of key id

List&lt;String&gt; ids = pojo.getMaps().stream()                 // Stream&lt;Map&lt;String, String&gt;&gt;
                                 .map(e -&gt; e.get(&quot;id&quot;))    // Stream&lt;String&gt; 
                                 .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年9月7日 19:05:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63776361.html
匿名

发表评论

匿名网友

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

确定