2017-04-03 53 views
-2

如何将Map.Entry<String, String>的列表转换为StringList.Map <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. 
+0

[为什么“有人能帮助我吗?”不是一个实际的问题?(HTTPS: //meta.stackoverflow.com/questions/284236/why-is -can-someone-help-me-not-an-actual-question) –

+1

@StefanWarminski现在好吗? –

+0

预期输出是什么?你有什么尝试? –

回答

0

这是什么意思?你想将地图元素(键=值)转换为字符串值?对于这一点,你可以使用地图条目集循环和读取键和地图元素的值,并创建自定义字符串和CONCAT所有的列表:

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 ; 
} 

的地图列表,你可以在列表定义循环:

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

thanx man it works,但是如果我想将它作为键值对添加到字符串数组中,该怎么办? –

+1

提示:字符串concatination是一个循环不是一个好的(高性能)的想法。使用'StringBuilder'代替 –

+1

@AnujVictor您可以将创建的字符串添加到列表中,并在循环后转换列表String [] entries = entryList.toArray(new String [entryList.size()]);' –

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(); 
+0

thanx好友..它现在清除给我。非常感谢 :) –

相关问题