Java映射值到逗号分隔的字符串

时间:2021-07-19 00:23:07

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

有没有一种有效的方法可以使用guava或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”。

7 个解决方案

#1


11  

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(","));

#2


9  

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

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

#3


2  

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"

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

请注意,我将HashMap更改为LinkedHashMap,以保持插入顺序。

#4


1  

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:

如果要将值显示为以逗号分隔的列表,可以使用以下命令:

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

Output:

输出:

[2, 4]

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

PS:如果需要,可以使用.replaceAll()删除[]

#5


1  

You can use makeString() in Eclipse Collections.

您可以在Eclipse集合中使用makeString()。

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集合的提交者。

#6


0  

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

#7


0  

Map has a function values() to get all values. map.values() are string type so pass to string joiner. Example:

Map有一个函数values()来获取所有值。 map.values()是字符串类型,因此传递给字符串joiner。例:

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

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

#1


11  

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(","));

#2


9  

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

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

#3


2  

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"

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

请注意,我将HashMap更改为LinkedHashMap,以保持插入顺序。

#4


1  

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:

如果要将值显示为以逗号分隔的列表,可以使用以下命令:

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

Output:

输出:

[2, 4]

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

PS:如果需要,可以使用.replaceAll()删除[]

#5


1  

You can use makeString() in Eclipse Collections.

您可以在Eclipse集合中使用makeString()。

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集合的提交者。

#6


0  

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

#7


0  

Map has a function values() to get all values. map.values() are string type so pass to string joiner. Example:

Map有一个函数values()来获取所有值。 map.values()是字符串类型,因此传递给字符串joiner。例:

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

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