Java Map Values to Comma Separated String
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
l a s
Updated on July 12, 2022Comments
-
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 over 10 yearsOr just values:
System.out.println (map.values());
-
everton over 10 yearsOr just keys! :-)
System.out.println(map.keySet());
-
Vinayak Dornala over 6 yearsThe method is available String.join is available from JDK 1.8
-
DavidS over 4 yearsIn Java 8 just use
String.join(",", testMap.values())
like in Syam's answer. -
lreeder about 4 yearsYes, that's MUCH cleaner. Everyone should upvote Syam's answer
-
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 andmakeValueString
for the value to a String value. The method namemakeString
is not more self-described one -
Donald Raab about 4 yearsGreat observation. These may be potential contributions to the library if you are interested in contributing. The method
makeString
on a map is actually inherited fromRichIterable
. In Eclipse Collections, aMutableMap<K, V>
extendsRichIterable<V>
. -
Admin over 2 yearsThis 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())));