public static void main(String[] args)
{
/**
* 在学生中有男孩和女孩,而男孩和女孩都有自己的编号和姓名
* 这里将学生存储到Map集合中,把男孩和女孩也存储到Map集合中
* 再将男孩和女孩集合嵌套进学生集合
*/
Map<Integer,String> boys = new HashMap<>();
Map<Integer,String> girls = new HashMap<>();
boys.put(1,"小明");
boys.put(2,"小强");
boys.put(3,"小龙");
girls.put(1,"小小");
girls.put(2,"小花");
girls.put(3,"小佳");
Map<Integer,Map<Integer,String>> Student = new HashMap<>();
Student.put(1,boys);
Student.put(2,girls);
System.out.println(Student);
//调用Student集合方法entrySet方法,将Student集合的键值对关系对象,存储到Set集合
Set<Map.Entry<Integer,Map<Integer,String>>> Stu = Student.entrySet();
//迭代器迭代Set集合
Iterator<Map.Entry<Integer,Map<Integer,String>>> Stu_it = Stu.iterator();
while (Stu_it.hasNext())
{
//Stu_it.next方法,取出的是Student集合的键值对关系对象
Map.Entry<Integer, Map<Integer, String>> Stu_entry = Stu_it.next();
Integer Stu_key = Stu_entry.getKey();
Map<Integer,String> Stu_value = Stu_entry.getValue();
System.out.println(Stu_key + "=" + Stu_value);
//调用Person集合entrySet方法,键值对关系对象存储Set集合
Set<Map.Entry<Integer,String>> S = Stu_value.entrySet();
//迭代Set集合
Iterator<Map.Entry<Integer,String>> it = S.iterator();
while (it.hasNext())
{
//获取boys和girls中键值对对象并将其取出
Map.Entry<Integer,String> entry = it.next();
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "="+ value);
}
}
//使用增强for进行遍历
for(Map.Entry<Integer, Map<Integer, String>> Stud : Student.entrySet())
{
Integer Stu__key = Stud.getKey();
Map<Integer,String> Stu__value = Stud.getValue();
System.out.println(Stu__key + "=" + Stu__value);
for(Map.Entry<Integer,String> M : Stu__value.entrySet())
{
Integer key_ = M.getKey();
String value_ = M.getValue();
System.out.println(key_ + "=" + value_);
}
}
}