Map集合嵌套 4种遍历方式
一、俗称集合嵌套就是集合套集合
二、分析 Map集合的嵌套
自定义一个类:
public class Person { private String name; private int age; public Person() { //有参和无参构造 super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int hashCode() { //这个地方写hashcode是为了不让重复元素存入 final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
//注意事项:①导包时注意
②正确使用 entryset和keyset关键字
③使用迭代器遍历时 注意先创建集合,增强for正常遍历即可(因map无下标 不可以使用普通for进行遍历)
package com.oracle.demo01; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class Demo7 { public static void main(String[] args) { // 大map HashMap<String, HashMap<Person, String>> oracle = new HashMap<String, HashMap<Person, String>>(); // 小map HashMap<Person, String> java1018 = new HashMap<Person, String>(); HashMap<Person, String> java1227 = new HashMap<Person, String>(); // 封装集合 //给小map赋值 用put关键字 java1018.put(new Person("C罗", 34), "踢足球"); java1018.put(new Person("詹姆斯", 35), "打篮球"); java1227.put(new Person("张继科", 28), "打乒乓球"); java1227.put(new Person("朱婷", 22), "打排球"); oracle.put("java1018", java1018); //给大map赋值 同样用put关键字 oracle.put("java1227", java1227); // entryset +iterator遍历 // 1.获取大的Map的大结婚猪呢个对象Set集合 Set<Map.Entry<String, HashMap<Person, String>>> bigentrys = oracle.entrySet();//上面大map里面写的泛型是什么这里就写什么 Iterator<Map.Entry<String, HashMap<Person, String>>> it = bigentrys.iterator(); while (it.hasNext()) { Map.Entry<String, HashMap<Person, String>> bigentry = it.next(); String bigkey = bigentry.getKey(); //获取大对象key值 HashMap<Person, String> smallMap = bigentry.getValue(); //获取大对象value值 Set<Entry<Person, String>> entrySet = smallMap.entrySet(); //创建小对象集合 Iterator<Entry<Person, String>> sit = entrySet.iterator(); while (sit.hasNext()) { Entry<Person, String> e = sit.next(); System.out.println(bigkey + "..." + e.getKey() + "..." + e.getValue()); } } System.out.println("========================="); // 2. entryset 增强for遍历 Set<Entry<String, HashMap<Person, String>>> entrySet = oracle.entrySet(); for (Entry<String, HashMap<Person, String>> e : entrySet) { String bigKey = e.getKey(); HashMap<Person, String> hashMap = e.getValue(); Set<Entry<Person, String>> entrySet2 = hashMap.entrySet(); for (Entry<Person, String> e5 : entrySet2) { System.out.println(bigKey + "..." + e5.getKey() + "..." + e5.getValue()); } } System.out.println("3.增强for========================="); // 3.增强for+keyset方法 Set<String> keySet = oracle.keySet(); for (String s : keySet) { HashMap<Person, String> h = oracle.get(s); Set<Person> h2 = h.keySet(); for (Person p : h2) { System.out.println(s + "..." + p + "..." + h.get(p)); } } System.out.println("4.迭代器=========================");
//4. 迭代器+keyset方法 Set<String> keySet2 = oracle.keySet(); Iterator<String> ite = keySet2.iterator(); while (ite.hasNext()) { String s = ite.next(); HashMap<Person, String> hashMap = oracle.get(s); Set<Person> keySet3 = hashMap.keySet(); Iterator<Person> it8 = keySet3.iterator(); while (it8.hasNext()) { Person p1 = it8.next(); System.out.println(s + "..." + p1 + "..." + hashMap.get(p1)); } } } }
欢迎各位大神指点和评论;