2017-04-03 72 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好友..它現在清除給我。非常感謝 :) –

相關問題