import java.util.*; public class MapTest3 { public static void main(String[] args) { // 联系嵌套集合 Map<String, List<String>> map = new HashMap<>(); List<String> cities1 = new ArrayList<>(); Collections.addAll(cities1, "南京市", "常州市", "扬州市"); map.put("江苏省", cities1); List<String> cities2 = new ArrayList<>(); Collections.addAll(cities2, "武汉市", "孝感市", "宜昌市"); map.put("湖北省", cities2); System.out.println(map); //{江苏省=[南京市, 常州市, 扬州市], 湖北省=[武汉市, 孝感市, 宜昌市]} System.out.println(map.get("江苏省")); //[南京市, 常州市, 扬州市] List<String> cities = map.get("湖北省"); cities.forEach(s -> System.out.println(s)); } }
集合的嵌套使用
最新推荐文章于 2024-09-02 22:33:56 发布