4

How would I extract the values (not keys) from a List<Map<String,String>>, and flatten it to a List<String>?

i.e. tried the following but doesn't work.

List<Map<String,String>> mapList = .... ;

List<String> valueList = mapList.stream()
                                .map(o -> o.getValue())
                                .collect(Collectors.toList());

I'd like to filter the result by a given key as well.

4 Answers 4

4

You mean :

List<String> valueList = mapList.stream()
        .flatMap(a -> a.values().stream())
        .collect(Collectors.toList());

Edit

What if I want to specify a key e.g. I have "id" and "firstName", but only want "firstName"

In this case you can use filter after the flatmap like so :

List<String> valueList = mapList.stream()
        .flatMap(a -> a.entrySet().stream())
        .filter (e -> e.getKey().equals("firstName"))
        .map(Map.Entry::getValue)
        .collect(Collectors.toList ());
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, just realised that this gets all the values, and I only need one set. What if I want to specify a key e.g. I have "id" and "firstName", but only want "firstName".
add a filter ``` List<String> valueList = mapList.stream() .flatMap( i -> i.entrySet ().stream () .filter (entry->entry.getKey ().equals ("firstName")) .map (entry->entry.getValue ())) .collect(Collectors.toList ()); ```
2

Use .flatMap:

List<Map<String,String>> mapList = new ArrayList<>();

Map<String, String> mapOne = new HashMap<>();
mapOne.put("1", "one");
mapOne.put("2", "two");

Map<String, String> mapTwo = new HashMap<>();
mapTwo.put("3", "three");
mapTwo.put("4", "four");

mapList.add(mapOne);
mapList.add(mapTwo);

List<String> allValues = mapList.stream()
    .flatMap(m -> m.values().stream())
    .collect(Collectors.toList()); // [one, two, three, four]

1 Comment

What if I want to specify a key e.g. I have "id" and "firstName", but only want "firstName". Right now this grabs all of the values (sorry mistake on my part...)
2

Try

    List<String> valueList = mapList.stream()
            .flatMap(map -> map.entrySet().stream())
            .filter(entry -> entry.getKey().equals("KEY"))
            .map(Map.Entry::getValue)
            .collect(Collectors.toList());

Comments

1

The object o you are trying to map to o.getValue() is of type Map (which does not have a function getValue()), not Map.Entry (which would have such a function). What you can is get a Collection of the values with the function o.values().

You can then get a Stream from that collection, and flatten the resulting Stream of Streams like this:

List<String> valueList = mapList.stream()
                         .map(o -> o.values().stream())
                         .flatMap(Function.identity())
                         .collect(Collectors.toList());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.