20

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

l a s
  • 3,836
  • 10
  • 42
  • 61

7 Answers7

31

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(","));
lreeder
  • 12,047
  • 2
  • 56
  • 65
  • 4
    In Java 8 just use `String.join(",", testMap.values())` like in [Syam's answer](https://stackoverflow.com/a/40014896/201891). – DavidS Nov 01 '19 at 17:57
  • Yes, that's MUCH cleaner. Everyone should upvote Syam's answer – lreeder Mar 07 '20 at 00:50
  • 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())));` –  Aug 05 '21 at 11:10
12

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

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
9

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
lreeder
  • 12,047
  • 2
  • 56
  • 65
Syam Elakapalli
  • 256
  • 3
  • 4
5

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.

ssssteffff
  • 964
  • 4
  • 16
2

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

everton
  • 7,579
  • 2
  • 29
  • 42
1

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.

Donald Raab
  • 6,458
  • 2
  • 36
  • 44
Craig P. Motlin
  • 26,452
  • 17
  • 99
  • 126
  • @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 – Prasanth Rajendran Apr 15 '20 at 18:54
  • 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` extends `RichIterable`. – Donald Raab Apr 15 '20 at 19:47
0

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

reduckted
  • 2,358
  • 3
  • 28
  • 37