7

I have the following map:

Map<DataFields, String> myMap;

But I need to convert it to the following:

Map<String, String> myMap;

My best feeble attempt which doesn't even compile is:

myMap.keySet().stream().map(k -> k.name()).collect(Collectors.toMap(k, v)
3
  • 3
    I think you might need to use entrySet() instead of keySet(). Commented Feb 27, 2018 at 18:43
  • not sure if FP reduces any efficiency. Another answer stackoverflow.com/questions/17208346/… Commented Feb 27, 2018 at 19:10
  • Also, not sure why you needed such a map. Wherever you have to use this map you could have effectively converted the key to string and then used. Commented Feb 27, 2018 at 19:11

4 Answers 4

9

You need to stream the entrySet() (so you have both the keys and the values), and collect them to a map:

Map<String, String> result =
    myMap.entrySet()
         .stream()
         .collect(Collectors.toMap(e -> e.getKey().name(), e -> e.getValue()));
Sign up to request clarification or add additional context in comments.

5 Comments

e -> e.getValue() replace with Entry::getValue
Eugene it's just a syntactic sugar. Any performance advantage of using Entry::getValue
@AdeshKumar first, unless you tag me with @, I can't tell you commented, second is that a question or an affirmation?
Yes, it's advantageous for both performance and readability to use method references where possible.
@AdeshKumar a lambda expression will create one more synthetic method, while method references will not, that's about the only difference (besides readability)
2
Map<String, String> result = myMap
    .entrySet() // iterate over all entries (object with tow fields: key and value)
    .stream() // create a stream
    .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue()));
        // collect to map: convert enum Key value toString() and copy entry value

1 Comment

I'd prefer to use name() over toString() which is the reason I'll accept the other answer.
2

Another way of doing same without Collectors helper. Using entryset will make it very easy to map.

  map.entrySet()
                .stream()
                .collect(
                        () -> new HashMap<String, String>(),
                        (Map newMap, Map.Entry<DataFields, String> entry) -> {
                            newMap.put(entry.getKey().name(), entry.getValue());
                        }
                        ,
                        (Map map1, Map map2) -> {
                            map.putAll(map2);
                        }
                );

Comments

2

A Java 8, succint way to do it (without streams):

Map<String, String> result = new HashMap<>();
myMap.forEach((k, v) -> result.put(k.name(), v));

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.