一、嵌套集合

1、ArrayList嵌套 ArrayList


  • ArrayList< ArrayList<String> >
  • Collection< ArrayList<Integer> >

2、Map嵌套 ArrayList


  • HashMap<String, ArrayList<Person>>
  • ArrayList< HashMap<String, String>>

3、Map集合嵌套


  • HashMap<String, HashMap<String,String>>
  • HashMap<String, HashMap<Person,String>>

4、代码实例

  • Map 集合嵌套keySet遍历
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集合方法keySet将键存储到Set集合
Set<Integer> S1 = Student.keySet();
//迭代Set集合
Iterator<Integer> Stu_it = S1.iterator();
while (Stu_it.hasNext()) {
//it.next获取出来的是Set集合元素,Student集合的键
Integer Stu_key = Stu_it.next();
//Student集合的方法get获取值,值是一个Map集合
Map<Integer, String> Stu_value = Student.get(Stu_key);
Set<Integer> S2 = Stu_value.keySet();
Iterator<Integer> it = S2.iterator();
while (it.hasNext()) {
Integer key = it.next();
String value = Stu_value.get(key);
System.out.println(key + "=" + value);
}
}
System.out.println("==================================");

//使用增强for进行遍历
for(Integer Stu__key : Student.keySet())
{
Map<Integer,String> Stu__value = Student.get(Stu__key);
for (Integer key : Stu__value.keySet())
{
String value = Stu__value.get(key);
System.out.println(key + "=" +value);
}
}
}
  • Map 集合嵌套entrySet遍历
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_);
}
}
}