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
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
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
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