Skip to content
Advertisement

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

I have an object with a list of maps. Each map looks like this:

JavaScript

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

JavaScript

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

JavaScript

Advertisement

Answer

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:

JavaScript

and go from there:

JavaScript

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:

JavaScript

with:

JavaScript

it’s about the same amount of code.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement