Java 将值映射到逗号分隔的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20787983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Map Values to Comma Separated String
提问by l a s
Is there an effective way to convert java map values to comma separated string using guava or StringUtils?
有没有一种有效的方法可以使用番石榴或 StringUtils 将 java 映射值转换为逗号分隔的字符串?
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".
寻找一种将 testMap 转换为 String -> "val1,val2" 的方法。
回答by everton
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:
好的,这是我的猜测,以防您想要这样的东西(您的问题中没有说明)。这是在没有 Guava 或 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:
如果要将 显示values
为逗号分隔列表,可以使用以下命令:
System.out.println(map.values().toString());
Output:
输出:
[2, 4]
PS: Remove the []
s with .replaceAll()
if you want
PS:如果需要,请删除[]
s.replaceAll()
回答by ssssteffff
You can use StringUtils
:
您可以使用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"
Notethat I changed the HashMap
to a LinkedHashMap
, in order to keep the insertion order.
请注意,我将 the 更改HashMap
为 a LinkedHashMap
,以保持插入顺序。
回答by Louis Wasserman
Guava: Joiner.on(',').join(map.values())
.
番石榴:Joiner.on(',').join(map.values())
。
回答by Craig P. Motlin
You can use makeString()
in Eclipse Collections.
您可以makeString()
在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
.
如果您无法将您Map
的类型转换为 Eclipse Collections 类型,则可以使用MapAdapter
.
MapAdapter.adapt(testMap).makeString();
Note:I am a committer for Eclipse collections.
注意:我是 Eclipse 集合的提交者。
回答by lreeder
Here's how to do it in Java 8+ (doesn't require Guava, StringUtils, or other external libraries):
以下是在 Java 8+ 中的操作方法(不需要 Guava、StringUtils 或其他外部库):
testMap.values().stream().map(Object::toString).collect(Collectors.joining(","));
回答by Ankit Lalan
If you are using spring MVC then this might work for you.
如果您使用的是 spring MVC,那么这可能对您有用。
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
输出:abc、def、ghi
回答by Syam Elakapalli
In Java 1.8, a join()method was added to the String class that can do this.
在 Java 1.8 中,一个join()方法被添加到可以执行此操作的 String 类中。
Also Map
has a function values()
to get all values. map.values() are string type so just pass to String.join. Example:
还Map
具有values()
获取所有值的功能。map.values() 是字符串类型,因此只需传递给 String.join。例子:
Map<String, String> testMap = new HashMap<>();
testMap.put("key1", "val1");
testMap.put("key2", "val2");
String valueString = String.join(",",map.values()); //val1,val2