๐ ์ค์ฒฉ Map ์ง๋ค๋ฆญ์ ๋ํด ํ์คํ์ง ์๋ค. ๋์ค์ ์์ ํ๋๋ผ๋ ๊ธฐ์กด์์ ๋ฅผ ์ด๋ ๊ฒ ๊ณ ์น๋ฉด ๋ฌธ์ ๊ฐ ์๊ธธ๋ ์ฐ์ ์ ๊ธฐ๋กํด๋๋ค.
public class Map_map_Generic {
static HashMap<String, HashMap<String, String>> phoneBook = new HashMap<>();
public static void main(String[] args) {
addPhoneNo("์น๊ตฌ", "์ฃฝ๋ถ์ธ1", "10");
addPhoneNo("์น๊ตฌ", "์ฃฝ๋ถ์ธ2", "11");
addPhoneNo("์น๊ตฌ", "์ฃฝ๋ถ์ธ3", "12");
addPhoneNo("ํ์ฌ", "์ฃฝ๋ถ์ธ7", "20");
addPhoneNo("ํ์ฌ", "์ฃฝ๋ถ์ธ8", "21");
addPhoneNo("ํ์ฌ", "์ฃฝ๋ถ์ธ9", "22");
addPhoneNo("๋๋ค", "์ฃฝ๋ถ์ธ4", "30");
addPhoneNo("์น๊ตฌ", "์ฃฝ๋ถ์ธ5", "13");
addPhoneNo("์๊ฐ", "40");
printList();
}
static void addPhoneNo(String groupName, String name, String tel) {
addGroup(groupName);
HashMap<String, String> group = phoneBook.get(groupName);
group.put(tel, name);
}
static void addGroup(String groupName) {
if (!phoneBook.containsKey(groupName))
phoneBook.put(groupName, new HashMap());
}
static void addPhoneNo(String name, String tel) {
addPhoneNo("๊ธฐํ", name, tel);
}
static void printList() {
Set<Map.Entry<String, HashMap<String, String>>> set = phoneBook.entrySet(); //๋ฐ๊นฅ์ชฝ Map
Iterator<Map.Entry<String, HashMap<String, String>>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<String, HashMap<String, String>> e = it.next(); //๋ฐ๊นฅ์ชฝ Map it
Set<Map.Entry<String, String>> subSet = e.getValue().entrySet(); //์์ชฝ Map
Iterator<Map.Entry<String, String>> subIt = subSet.iterator();
System.out.println(" * " + e.getKey() + "[" + subSet.size() + "]");
while (subIt.hasNext()) {
Map.Entry<String, String> subE = subIt.next(); //์์ชฝ Map it
String telNo = (String) subE.getKey();
String name = (String) subE.getValue();
System.out.println(name + " " + telNo);
}
System.out.println();
}
}
}
์ถ์ฒ : ์๋ฐ์ ์ ์