MAP集合案例

image

点击查看代码
public class Test4 {
    public static void main(String[] args) {
        HashMap<String, ArrayList<String>> hm = new HashMap<>();

        ArrayList<String> cityList1 = new ArrayList<>();
        ArrayList<String> cityList2 = new ArrayList<>();
        ArrayList<String> cityList3 = new ArrayList<>();

        Collections.addAll(cityList1, "南京市", "扬州市", "苏州市", "无锡市", "常州市");
        Collections.addAll(cityList2, "武汉市", "孝感市", "十堰市", "宜昌市", "鄂州市");
        Collections.addAll(cityList3, "石家庄市", "唐山市", "邢台市", "保定市", "张家口市");

        hm.put("江苏省", cityList1);
        hm.put("湖北省", cityList2);
        hm.put("河北省", cityList3);

        Set<String> keys = hm.keySet();
        for (String key : keys) {
            ArrayList<String> list = hm.get(key);
            StringJoiner sj = new StringJoiner(",");
            for (String city : list) {
                sj.add(city);
            }
            System.out.println(key + " = " + sj);
        }
    }
}
posted @ 2025-09-06 14:41  lachii  阅读(6)  评论(0)    收藏  举报