romach
4/27/2017 - 7:39 PM

Collect Map to String

Collect Map to String

public static void main(String[] args) throws Exception {
    Map<Integer, String> map = new HashMap<>();
    map.put(1, "foo");
    map.put(2, "bar");
    map.put(3, "baz");
    String result = map.entrySet()
        .stream()
        .map(entry -> entry.getKey() + " - " + entry.getValue())
        .collect(Collectors.joining(", "));
    System.out.println(result);
}