package com.itheima_01;
//在学习的过程中对集合方法的调用总是出现错误
//错误点:方法entryset的调用和方法getset调用的混淆
//解决方法:通过老师给的思路,了解产生的集合是键对对集合,还是键的集合,如果是键的集合,就用键去获得value值,如果是键对对集合,就用集合对象去调用方法获得键(key)的值和调用方法获得对应的value值
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapIncludeArrayListTest {
private static final Object string = null;
public static void main(String[] args) {
HashMap<String, ArrayList<String>> hm=new HashMap<String, ArrayList<String>>();
ArrayList<String> al1=new ArrayList<String>();
al1.add("诸葛亮");
al1.add("赵云");
ArrayList<String> al2=new ArrayList<String>();
al2.add("唐僧");
al2.add("孙悟空");
ArrayList<String> al3=new ArrayList<String>();
al3.add("武松");
al3.add("鲁智深");
hm.put("三国演义", al1);
hm.put("西游记", al2);
hm.put("水浒传", al3);
[color=Red]Set<Entry<String, ArrayList<String>>> entrySet = hm.entrySet();
[color=Red]for (Entry<String, ArrayList<String>> entry : entrySet) {
String key = entry.getKey();
System.out.println(key);
ArrayList<String> value = entry.getValue();
for (String string : value) {
System.out.println("\t"+string);
}
}
Set<String> keySet = hm.keySet();
for (String string : keySet) {
ArrayList<String> arrayList = hm.get(string);
System.out.println(string);
for (String string2 : arrayList) {
//ArrayList<String> arrayList2 = hm.get(string2);
System.out.println("\t"+string2);
}
}
}
}
|
|