hashmap的嵌套遍历

hashmap的嵌套,存储一个自定义类

/*
* hashmap的嵌套,存储一个自定义类

*分析:
* 1.创建一个hashmap,且它的键和值的其中一个也是hashmap
* 2.创建两个hashmap,它的键 是自定义类Student
* 3.创建Student对象,并分别向第二步的两个hashmap添加
* 4.分两种方法遍历输出
*/

 

自定义类:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

public class Student {

     

    private String name;

    private int age;

    public Student() {

        super();

    }

    public Student(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 String toString() {

        return "Student [name=" + name + ", age=" + age + "]";

    }

 

     

     

 

}

  hashmap类:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

import java.util.Collection;

import java.util.HashMap;

import java.util.Map.Entry;

import java.util.Set;

/**

 * hashmap的嵌套

 * @author ma

 *

 */

public class HashMapDemo6 {

    /*

     * hashmap的嵌套,存储一个自定义类

     *

     *分析:

     *  1.创建一个hashmap,且它的键和值的其中一个也是hashmap

     *  2.创建两个hashmap,它的键 是自定义类Student

     *  3.创建Student对象,并分别向第二步的两个hashmap添加

     *  4.分两种方法遍历输出

     */

     

    public static void main(String[] args) {

         

        //声明一个嵌套的hashmap对象hmhm

        HashMap<String, HashMap<Student, String>> hmhm = new HashMap<String, HashMap<Student,String>>();

         

        //声明一个hashmap对象hm1

        HashMap<Student, String> hm1 = new HashMap<Student, String>();

         

        //声明两个学生对象

        Student s1 = new Student("张三",1);

        Student s2 = new Student("李四",2);

         

        //把对象放入hm1

        hm1.put(s1, "1");

        hm1.put(s2, "2");

         

        //声明一个hashmap对象hm2

        HashMap<Student, String> hm2 = new HashMap<Student, String>();

         

        //声明两个学生对象

        Student s3 = new Student("赵大",3);

        Student s4 = new Student("王二",4);

         

        //把对象放入hm2

        hm1.put(s3, "3");

        hm1.put(s4, "4");

         

        //把hm1和hm2放入hmhm

        hmhm.put("1", hm1);

        hmhm.put("2", hm2);

         

        //遍历输出1

        Set<Entry<String, HashMap<Student, String>>> set = hmhm.entrySet();

        for (Entry<String, HashMap<Student, String>> entry : set) {

             

            Set<Entry<Student, String>> entry1 = entry.getValue().entrySet();

            for (Entry<Student, String> entry2 : entry1) {

                System.out.println(entry2);

            }

        }

         

        System.out.println("-------------------------------------------------");

         

        //遍历输出2

        Collection<HashMap<Student, String>> collection = hmhm.values();

        for (HashMap<Student, String> hashMap : collection) {

                 

                for(Student s : hashMap.keySet()){

                    System.out.println(s);

                }

        }

         

    }

     

}

  输出结果:

    Student [name=李四, age=2]=2
    Student [name=王二, age=4]=4
    Student [name=赵大, age=3]=3
    Student [name=张三, age=1]=1
-------------------------------------------------
    Student [name=李四, age=2]
    Student [name=王二, age=4]
    Student [name=赵大, age=3]
    Student [name=张三, age=1]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值