- package it.cast;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Set;
- public class HashMap_ArrayList {
- public static void main(String[] args) {
- ArrayList<String> list1 = new ArrayList<>();
- ArrayList<String> list2 = new ArrayList<>();
- ArrayList<String> list3 = new ArrayList<>();
- HashMap<String, ArrayList<String>> map = new HashMap<>();
- map.put("三国演义", list1);
- map.put("水浒传", list2);
- map.put("西游记", list3);
- list1.add("曹操");
- list1.add("刘备");
- list2.add("宋江");
- list2.add("鲁智深");
- list3.add("孙悟空");
- list3.add("猪八戒");
- Set<String> keySet = map.keySet();
- for (String string : keySet) {
- System.out.println(string);
- ArrayList<String> list4 = map.get(string);
- Iterator<String> iterator = list4.iterator();
- while (iterator.hasNext()) {
- String string2 = (String) iterator.next();
- System.out.println("\t" + string2);
- }
- }
- }
- }
复制代码
|
|