Convert Java Map Enum keys to Map String keys

jiveturkey :

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

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()));

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=474139&siteId=1