进阶day04-集合的嵌套
This commit is contained in:
30
javaSE-day04/src/com/inmind/map01/MapDemo06.java
Normal file
30
javaSE-day04/src/com/inmind/map01/MapDemo06.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.inmind.map01;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/*
|
||||
集合的嵌套
|
||||
需求:定义一个双列集合展示对应省份,拥有的城市
|
||||
江苏省:苏州 ,无锡,常州,南京,扬州
|
||||
*/
|
||||
public class MapDemo06 {
|
||||
public static void main(String[] args) {
|
||||
//1.定义一个Key为String,Value为Set集合的双列集合
|
||||
Map<String, Set<String>> map = new HashMap<>();
|
||||
|
||||
HashSet<String> sets1 = new HashSet<>();
|
||||
sets1.add("苏州");
|
||||
sets1.add("无锡");
|
||||
sets1.add("常州");
|
||||
sets1.add("南京");
|
||||
map.put("江苏省", sets1);
|
||||
System.out.println(map);
|
||||
|
||||
HashSet<String> sets2 = new HashSet<>();
|
||||
Collections.addAll(sets2,"武汉","孝感","宜昌");
|
||||
map.put("湖北省", sets2);
|
||||
|
||||
System.out.println(map);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user