Map<Integer, String> wordsByKey = new HashMap<>(); wordsByKey.put(1, "one"); wordsByKey.put(2, "two"); wordsByKey.put(3, "three"); wordsByKey.put(4, "four");
public String convertWithStream(Map<Integer, ?> map) { String mapAsString = map.keySet().stream() .map(key -> key + "=" + map.get(key)) .collect(Collectors.joining(", ", "{", "}")); return mapAsString; }
public Map<String, String> convertWithStream(String mapAsString) { Map<String, String> map = Arrays.stream(mapAsString.split(",")) .map(entry -> entry.split("=")) .collect(Collectors.toMap(entry -> entry[0], entry -> entry[1])); return map; }