Java Map Values to Comma Separated String

46.6k views Asked by At

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

8

There are 8 answers

2
everton On

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

0
ssssteffff On

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.

0
Louis Wasserman On

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

2
Craig P. Motlin On

You can use makeString() in Eclipse Collections.

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

String commaAndSpace = testMap.makeString();
Assert.assertTrue(commaAndSpace.equals("val1, val2")
  || commaAndSpace.equals("val2, val1"));

String comma = testMap.makeString(",");
Assert.assertTrue(comma.equals("val1,val2")
  || comma.equals("val2,val1"));

If you cannot convert your Map to an Eclipse Collections type, you can use MapAdapter.

MapAdapter.adapt(testMap).makeString();

Note: I am a committer for Eclipse collections.

3
lreeder On

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(","));
0
Ankit Lalan On

If you are using spring MVC then this might work for you.

import org.springframework.util.StringUtils;

Map<String, String> groups = //List of Maps
List<String> targetList = new ArrayList<String>(groups).values());
String csv = StringUtils.arrayToCommaDelimitedString(targetList.toArray());

Output: abc,def,ghi

1
Syam Elakapalli On

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
0
murtiko On

I think sorting may be important here. Not sure if map.values() returns in deterministic order, so i would prefer this:

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

String formatted = map.keySet().stream().sorted()
                .map(k -> map.getOrDefault(k, "").toString())
                .collect(Collectors.joining(","));

At least this sorts by keys and is a bit more safe?