如何将一个String类型的数据转化为Map类型

将下面这个String数据转化为Map。

String mapStr = {Papua New Guinea=166, Cambodia=109, Kazakhstan=117,Saint Martin=297, Cocos (Keeling) Islands=36, Malawi=147, Andorra=1, Liechtenstein=121, Poland=169, Bulgaria=22, Jordan=105, Tunisia=211, United Arab Emirates=2, Tuvalu=215, Kenya=107, French Polynesia=165, Brunei=27, Djibouti=54, Lebanon=119, Azerbaijan=16, Cuba=48, Czech Republic=52, Serbia=180, Bosnia and Herzegovina=17, Niger=153, Rwanda=182, French Southern Territories=204, Bangladesh=19, Barbados=18, Nicaragua=156, Norway=158, Botswana=32, Saint Vincent and the Grenadines=276, Macao=139, Denmark=55, Dominican Republic=57, Mexico=148, Uganda=219, Micronesia=69, Suriname=197, Greenland=79,Saint Helena=189}'

用key,value的形式进行分割,然后存入一个新的map
中去。

private Map<String,String> countryMap = new HashMap<String,String>();
String cms =mapStr.replace("{", "").replace("}", "");
String[] countryMapStr = cms.split(",");
for(String  s:countryMapStr ){
            System.err.println("s:"+s);
            String[] ms = s.split("=");
            countryMap.put(ms[0], ms[1]);
        }

你可能感兴趣的:(数据结构)