Map.Entry <string,string>列表

时间:2017-04-03 06:06:22

标签: java list

如何将Map.Entry<String, String>列表转换为String

List<Map.Entry<String, String>> : [AREA_DS_ID=1,5,9,13,17,21,25,29,33, PROJECTS_ID=13,78,267,18,28,33,55,99, SIGNAL_NAME=a, ASSESSMENTNAME=a]
// these are the values which I need to convert into String.

2 个答案:

答案 0 :(得分:0)

你的意思是什么?你想将map元素(key = value)转换为字符串值???为此,您可以在地图条目集上使用循环并读取地图元素的键和值,并创建自定义字符串并连接所有列表:

String resultString ="" ;
for(Map.Entry<String,String> entry : map.EntrySet())
{
   String key = entry.getKey();
   String value = entry.getValue();  
   // create custom string for each map element
   String testString = key + "=" + value;
   resultString +=  testString ;
}

对于Map列表,您可以在列表中定义循环:

for(Map<String,String> map : list)
{
  // use map such above code
  ...
}

答案 1 :(得分:0)

正如您对评论的回答:我只是尝试过,并且根本没有例外:

Map<String, String> map = new LinkedHashMap<>();
map.put("AREA_DS_ID", "1,5,9,13,17,21,25,29,33");
map.put("PROJECTS_ID", "13,78,267,18,28,33,55,99");
map.put("SIGNAL_NAME", "a");
map.put("ASSESSMENTNAME", "a");

List<String> entryList = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (Entry<String, String> entry : map.entrySet()) {
    String value = entry.getKey() + '=' + entry.getValue();
    entryList.add(value);
    sb.append(value);
}
String[] entries = entryList.toArray(new String[entryList.size()]);
String mapAsString = sb.toString();