0

I have a Map<String, List<Object>> , I would like to convert it to Map<String, List<String>> in java 8 .

In Java 1.7 I was using a function to extract value from Map and then wrote another method declared empty List , used for loop to extract each element and then added to List Object in java 1.7

Any suggestions to do same in java 8 using lambda expressions

Thanks in advance

1
  • What are these objects? How do you intend to convert them to strings? Commented Apr 4, 2019 at 4:31

1 Answer 1

1

You can use this:

Map<String, List<String>> result = map.entrySet().stream()
        .map(e -> Map.entry(e.getKey(),
                e.getValue().stream()
                        .map(o -> (String) o) //or Object::toString depending on the object
                        .collect(Collectors.toList())))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Map.entry is from Java 9, you can use AbstractMap.SimpleEntry if you are on Java 8.

Sign up to request clarification or add additional context in comments.

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.