How to transform the key and value of a each entry set of a Map using Java 8?

hashmap, java, java-8, java-stream

Solution

It looks like what you really want is

 myMap.entrySet()
     .stream()
     .collect(Collectors.toMap(
         e -> new Type1(e.getKey()), e -> new Type2(e.getValue())));

though I admit it's honestly difficult to tell.

Problem

I have a `Map<String, String>` that I want to transform to a `Map<Type1,Type2>` using Java streams. This is what I tried but I think I am getting the syntax wrong: ``` myMap.entrySet() .stream() .collect(Collectors.toMap(e -> Type1::new Type1(e.getKey()), e -> Type2::new Type2(e.getValue)))); ``` Also tried ``` myMap.entrySet() .stream() .collect(Collectors.toMap(new Type1(Map.Entry::getKey), new Type2(Map.Entry::getValue)); ``` But I just keep running compile errors. How do I do this transform?

Original source