java将数据从List转换Map

/**
* 1个国家下所有城市,一对多关系
* 组装成Map结构返回
**/
public Map<String,List<JSONObject>> getCountry(){
    List<JSONObject> countryList = countryService();
    Map<String,List<JSONObject>> map = new HashMap<>();
    for(int i = 0;i<countryList.size();i++){
        JSONObject jsonObjet = countryList.get(i);
        if(map.containsKey(countryId)) {
            List<JSONObject> list = map.get(countryId);
            list.add(jsonObjet);
        }else{
            List<JSONObject> list = new ArrayList<JSONObject>();
            list.add(jsonObjet);
            map.put(countryId,list);
        }
    }
    return map;
}

猜你喜欢

转载自www.cnblogs.com/chenweichu/p/9074511.html