-3
map.values().stream().distinct().forEach(System.out::print);

I am not able to add comma in the above code for adding a comma between the values of the hashtable

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Shiga S
  • 11
  • 1

2 Answers2

2

If I understood you correctly then you want to store all the values of the map into a comma separated string so You can use String.join(",",list);

The second argument above is the list of strings which is your map values

1

You can do it as follows:

import java.util.Map;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        // An example map
        Map<Integer, String> map = Map.of(1, "One", 2, "Two", 3, "Three");

        // Join the values using comma as the delimiter
        String values = map.values().stream().distinct().collect(Collectors.joining(","));

        // Print
        System.out.println(values);
    }
}

Output:

Three,Two,One
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110