简体   繁体   English

具有键值对的单独字符串

[英]Separate String with Key value Pair

I've a string array which has got key and value which are separated using a delimiter. 我有一个字符串数组,它具有使用定界符分隔的键和值。 I need to separate the key value pair and move it to a HashMap. 我需要分离键值对并将其移动到HashMap。

The string is as follows. 字符串如下。

String[] str={"12345$abcd","12332$abcgd","5555$afdsd"};
/*
 I need to move it to HashMap as key-value pairs
 12345=abcd
 12332=abcgd
 5555=afdsd
*/

Can someone please help me to do this in the most efficient way? 有人可以帮助我以最有效的方式做到这一点吗?

String[] str={"12345$abcd","12332$abcgd","5555$afdsd"};

Map<String, String> map = new HashMap<String, String>();
for(final String s : str) {
   final String split[] = s.split("\\$");
   map.put(split[0], split[1]);
}
String[] str={"12345$abcd","12332$abcgd","5555$afdsd"};
Map<String, String> mp = new HashMap<String, String>();
for(String string: str){
mp.put(string.split("\\$")[0],string.split("\\$")[1]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM