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

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

Create arraylist from list of maps for certain key value with java 8

问题

以下是翻译好的内容:

我有一个包含映射列表的对象。每个映射如下所示:

  1. {
  2. id: "something",
  3. link: "someLink"
  4. }

我想要创建一个包含所有映射中 id 的数组列表。我可以使用一个简单的循环来实现:

  1. List<String> Ids = new ArrayList<>();
  2. List<Map<String, String>> maps = pojo.getMaps();
  3. for (Map<String, String> map : maps) {
  4. Ids.add(map.get("id"));
  5. }

但是如何在Java 8的流式处理中用一行代码完成呢?我从未在映射上使用过它,所以感到困惑。
我猜大致的思路是这样的,但实际上我不太确定:

  1. 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:

  1. {
  2. id: &quot;something&quot;,
  3. link: &quot;someLink&quot;
  4. }

I am trying to create an array list of all the id's in the maps. I can do this with a simple loop

  1. List&lt;String&gt; Ids = new ArrayList&lt;&gt;();
  2. List&lt;Map&lt;String, String&gt;&gt; maps = pojo.getMaps();
  3. for(Map&lt;String, String&gt; map: maps) {
  4. Ids.add(map.get(&quot;id&quot;));
  5. }

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

  1. List&lt;String&gt; ids = pojo.getMaps().stream().map(Map.Entry:: ???? ).collect(Collectors.toList())

答案1

得分: 4

  1. &gt; `pojo.getMaps().stream()`
  2. 到目前为止,一切都很好。您现在有一个 `Map&lt;String, String&gt;` 对象的流。我们只需要从中获取键。因此,对于你奇怪的地图对象之一,我们如何将其转化为键值呢?
  3. 看起来一个简单的 `map.get(&quot;id&quot;)` 就可以完成这个工作,是吗?
  4. 所以让我们将其放入lambda表达式中:
  5. `pojo.getMaps().stream().map(theMap -&gt; theMap.get(&quot;id&quot;))`
  6. 现在我们有一个带有id `Stream&lt;String&gt;`
  7. ## 但是,重要的一点!
  8. 您一开始就拥有一个看起来像对象的映射对象,这是一个巨大的代码异味。很可能你应该回到项目的前几步来修复这个问题。您确实应该有一个表示此链接概念的类:

@Value
public class Link {
String id, link;
}

  1. 然后从这里开始:

listOfLinks.stream().map(Link::getId).distinct().collect(...);

  1. 注意:上面的示例使用了[Lombok`@Value`](https://projectlombok.org/features/Value)。
  2. ## 函数式只是一种工具
  3. 请注意,没有必要重新编写您的代码以“使用java 8功能”。比较:
  1. var ids = pojo.getMaps().stream()
  2. .map(m -&gt; m.get(&quot;id&quot;))
  3. .collect(Collectors.toList());
  1. 与:
  1. var ids = new ArrayList&lt;String&gt;();
  2. for (var m : pojo.getMaps()) ids.add(m.get(&quot;id&quot;));
  1. 两者的代码量几乎相同。
英文:

> 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:

  1. @Value
  2. public class Link {
  3. String id, link;
  4. }

and go from there:

  1. 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:

  1. var ids = pojo.getMaps().stream()
  2. .map(m -&gt; m.get(&quot;id&quot;))
  3. .collect(Collectors.toList());

with:

  1. var ids = new ArrayList&lt;String&gt;();
  2. 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 的值。

  1. List&lt;String&gt; ids = pojo.getMaps().stream()
  2. .map(e -&gt; e.get(&quot;id&quot;))
  3. .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

  1. List&lt;String&gt; ids = pojo.getMaps().stream() // Stream&lt;Map&lt;String, String&gt;&gt;
  2. .map(e -&gt; e.get(&quot;id&quot;)) // Stream&lt;String&gt;
  3. .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:

确定