Java Map Values to Comma Separated String

34,550

Solution 1

Here's how to do it in Java 8+ (doesn't require Guava, StringUtils, or other external libraries):

testMap.values().stream().map(Object::toString).collect(Collectors.joining(","));

Solution 2

Guava: Joiner.on(',').join(map.values()).

Solution 3

In Java 1.8, a join() method was added to the String class that can do this.

Also Map has a function values() to get all values. map.values() are string type so just pass to String.join. Example:

Map<String, String> testMap = new HashMap<>();
testMap.put("key1", "val1");
testMap.put("key2", "val2");

String valueString = String.join(",",map.values()); //val1,val2

Solution 4

You can use StringUtils:

final Map<String, String> testMap = new LinkedHashMap<String, String>();
testMap.put("key1", "val1");
testMap.put("key2", "val2");

String csv = StringUtils.join(testMap.values(), ',')); // "val1,val2"

Note that I changed the HashMap to a LinkedHashMap, in order to keep the insertion order.

Solution 5

OK, here's my guess in case you want something like this (there's no clarification in your Question). This is done without Guava or StringUtils:

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

map.put(1, 2);
map.put(3, 4);

System.out.println(map.toString());

Output:

{1=2, 3=4}

If you want to display the values as a comma separated list, you could use this:

System.out.println(map.values().toString());

Output:

[2, 4]

PS: Remove the []s with .replaceAll() if you want

Share:
34,550
l a s
Author by

l a s

Updated on July 12, 2022

Comments

  • l a s
    l a s almost 2 years

    Is there an effective way to convert java map values to comma separated string using guava or StringUtils?

    Map<String, String> testMap = new HashMap<>();
    testMap.put("key1", "val1");
    testMap.put("key2", "val2");
    

    looking for a way to convert testMap to a String -> "val1,val2".

  • Akber Choudhry
    Akber Choudhry over 10 years
    Or just values: System.out.println (map.values());
  • everton
    everton over 10 years
    Or just keys! :-) System.out.println(map.keySet());
  • Vinayak Dornala
    Vinayak Dornala over 6 years
    The method is available String.join is available from JDK 1.8
  • DavidS
    DavidS over 4 years
    In Java 8 just use String.join(",", testMap.values()) like in Syam's answer.
  • lreeder
    lreeder about 4 years
    Yes, that's MUCH cleaner. Everyone should upvote Syam's answer
  • Prasanth Rajendran
    Prasanth Rajendran about 4 years
    @Donald Raab, I would have been great if the method name of MutableMap#makeString is something like makeKeyString for the key to a String value and makeValueString for the value to a String value. The method name makeString is not more self-described one
  • Donald Raab
    Donald Raab about 4 years
    Great observation. These may be potential contributions to the library if you are interested in contributing. The method makeString on a map is actually inherited from RichIterable. In Eclipse Collections, a MutableMap<K, V> extends RichIterable<V>.
  • Admin
    Admin over 2 years
    This can be useful when you're working with more complex objects, e.g: a symbols TreeMap ... symbols.entrySet().stream().filter(x -> x.getValue().myParameter.size() > 0).map(Map.Entry::getKey).collect(Collectors.toList())));