Map集合遍历的4种方式以及Map集合嵌套的遍历方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40285302/article/details/82948893

public class traverseMap {
    public static void main(String[] args) {
        HashMap<Integer,String> has = new HashMap<>();
        has.put(1,"张三");
        has.put(2,"李四");
        has.put(3,"王五");
        show1(has);
        show2(has);
    }

    /*
        使用keySet()对Map集合进行遍历  Set<K> keySet() K-->键的数据类型
        将集合的键存放到Set集合中,再对键进行遍历
        通过Map集合中的键找值的方法get(key)获取到值

        遍历方式分为foreach循环和while循环
            |--foreach循环也就是对通过keySet()方法获得的键Set集合进行遍历
            |--while循环则是通过创建一个Iterator迭代器对象进行遍历,再通过next()去获取每个key,随之获取值.
        总结:反正keySet()就是通过键找值的方法去遍历
     */
    private static void show2(HashMap<Integer, String> has) {
        //通过foreach循环遍历
        System.out.println("=========通过keySet()方法用foreach循环遍历");
        Set<Integer> keys = has.keySet();
        for (Integer key : keys) {
            String value = has.get(key);//通过键getValue()
            System.out.println(key + " = " + value);
        }

        System.out.println("=========通过keySet()方法用迭代器循环遍历");
        Set<Integer> keysIter = has.keySet();
        Iterator<Integer> it = keysIter.iterator();
        while(it.hasNext())
        {
            Integer key = it.next();   //通过next()取到每次遍历的key
            String value = has.get(key); //通 过key用get()方法获取到value
            System.out.println(key + " = " + value);
        }
    }

    /*
        通过entrySet()方法对Map进行遍历,将每个键值对封装为对象,再通过对象去getValue()和getKey()
        Set<Map.Entry<K,v>>   K-->键的数据类型  V-->值的数据类型
        遍历方式分为foreach和while
            |--foreach循环先需要通过entrySet()方法把所有的键值对封装为对象
               再通过此循环对每个entry对象进行getKey()和getValue()
            |--while循环同上也是先通过entrySet()方法把所有的键值对封装为对象
               再通过获取到的entry对象创建一个Iterator对象(迭代的泛型和上面的Set<...>里面的泛型一致),
               通过while循环进行遍历,通过迭代器中的next()方法获取每个对象,再通过获取到的对象getKey()和getKey()
         总结:更加的贴合面向对象

     */
    private static void show1(HashMap<Integer, String> has) {
        Set<Map.Entry<Integer, String>> entries = has.entrySet();  //将所有的键值对封装为对象

        //通过foreach循环   ,前面的遍历的类型是和上面Set集合中的泛型是一样的
        System.out.println("==========通过entrySet()方法foreach循环进行遍历");
        for (Map.Entry<Integer, String> entry : entries) {
            Integer key = entry.getKey();    //获取到键
            String value = entry.getValue(); //获取到值
            System.out.println(key + " = " + value);
        }

        //通过while循环,需要创建迭代器,通过迭代器对entries对象进行遍历
        System.out.println("=========通过entrySet方法while循环进行遍历");
        Set<Map.Entry<Integer,String>> entries1 = has.entrySet();
        Iterator<Map.Entry<Integer,String>> it = entries1.iterator();
        while(it.hasNext())
        {
            Map.Entry<Integer, String> entry = it.next();  //获取到每次遍历的对象
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + " = " + value);

        }

    }
}
Map集合中嵌套Map集合
/*
    集合的嵌套
        Map集合中嵌套Map集合
     但是在Map集合中嵌套Map集合的意义不大,不贴合面向对象

 */
public class MapNest01 {
    public static void main(String[] args) {
        HashMap<String,HashMap<String,Integer>> has = new HashMap<>();
        //创建就业班级的集合
        HashMap<String,Integer> hasJob = new HashMap<>();
        hasJob.put("张三",85);
        hasJob.put("李四",75);
        hasJob.put("王五",95);

        //创建基础班的集合
        HashMap<String,Integer> hasBas = new HashMap<>();
        hasBas.put("张三",85);
        hasBas.put("李四",75);
        hasBas.put("王五",95);

        has.put("就业班",hasJob);
        has.put("基础班",hasBas);
        System.out.println("========通过keySet()对集合进行遍历");
        show01(has);
        System.out.println("========通过entrySet()对集合进行遍历");
        show02(has);
    }

    //对集合进行遍历使用keySet()方法遍历嵌套的集合
    private static void show02(HashMap<String, HashMap<String, Integer>> has) {
        Set<String> keys = has.keySet();
        for (String key : keys) {

            System.out.print(key + " :  ");
            //通过遍历的key取到里面的值,而里面的值就是此key可以所对应的集合(也就是"就业班"对应的hasJob集合...)
            HashMap<String,Integer> hasVal = has.get(key);
            //取到的集合也需要进行遍历 ,就采用最简单的foreach循环
            for (String keyVal : hasVal.keySet()) {
                Integer valVal = hasVal.get(keyVal);   //通过获取到的集合中的键再获取到值
                System.out.println(keyVal + " = " + valVal);
            }
        }
    }

    /*
        通过entrySet()集合将里面的键值对封装为对象,再通过对象getKey()和getValue()
        而getValue()获取到的值就是has集合里面嵌套的一个HashMap集合
        在对此集合进行遍历(继续可以用foreach循环的方法或者迭代器都可以)
     */

    private static void show01(HashMap<String, HashMap<String, Integer>> has) {
        //将has集合中的键值对通过entrySet()封装为对象
        Set<Map.Entry<String , HashMap<String , Integer>>> entries = has.entrySet();
        //通过增强for循环对其进行遍历,获取到里面的键,再通过键获取到里面的值,
        //而里面的值就是嵌套的集合,所以需要对嵌套的集合继续进行遍历
        for (Map.Entry<String, HashMap<String, Integer>> entry : entries) {
            String key = entry.getKey();
            HashMap<String, Integer> hasValue = entry.getValue();   //此value是一个嵌套的集合,需要继续遍历
            for (String keyVal : hasValue.keySet()) {
                Integer value = hasValue.get(keyVal);
                System.out.println(keyVal + " = " + value);
            }
        }
    }
}
Map集合中嵌套List集合(更加的贴合面向对象)
public class MapNest02 {
    public static void main(String[] args) {

        HashMap<String,ArrayList<Person>> hashMap = new HashMap<>();

        ArrayList<Person> arrJob = new ArrayList<>();
        Person p1 = new Person("张三",18,89);
        Person p2 = new Person("李四",22,78);
        arrJob.add(p1);
        arrJob.add(p2);
        ArrayList<Person> arrBas = new ArrayList<>();
        Person p3 = new Person("王五",33,56);
        Person p4 = new Person("赵六",35,66);
        arrBas.add(p3);
        arrBas.add(p4);

        hashMap.put("就业班",arrBas);
        hashMap.put("基础班",arrJob);
        show02(hashMap);
        show01(hashMap);

    }

    /*
        通过keySet()方法将key
     */
    private static void show02(HashMap<String, ArrayList<Person>> hashMap) {
        System.out.println("===========通过keySet()方法");
        Set<String> keys =  hashMap.keySet();
        for (String key : keys) {
            System.out.println(key + " : ");
            ArrayList<Person> people = hashMap.get(key);  //通过遍历到的hashMap集合的键获取到值
            for (Person person : people) {
                System.out.println("姓名: " + person.getName() + "  年龄: " + person.getAge() + "  分数:" + person.getSecore());

            }
        }
    }
    //通过entrySet()方法    
    private static void show01(HashMap<String, ArrayList<Person>> hashMap) {
        System.out.println("===========通过entrySet()方法");
        Set<Map.Entry<String, ArrayList<Person>>> entries = hashMap.entrySet();
        //对此对象进行遍历
        for (Map.Entry<String, ArrayList<Person>> entry : entries) {
            String key = entry.getKey();
            System.out.println(key + " : ");
            ArrayList<Person> value = entry.getValue();   //这个获取到的就是一个对象
            //对里面的嵌套集合继续进行遍历
            for (Person person : value) {
                System.out.println("姓名 : " + person.getName() + " , 年龄: " + person.getAge() + " ,分数:" + person.getSecore());
            }
        }
    }
}
//Person类
public class Person {
    private String name;
    private int age;
    private int secore;

    public Person() {
    }

    public Person(String name, int age, int secore) {
        this.name = name;
        this.age = age;
        this.secore = secore;
    }

    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;
    }

    public int getSecore() {
        return secore;
    }

    public void setSecore(int secore) {
        this.secore = secore;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40285302/article/details/82948893