Bài 297: EnumMap trong java

Ngày đăng: 1/7/2023 9:26:02 AM

Hierarchy của lớp EnumMap

EnumMap trong java


Khai báo của lớp EnumMap

Dưới đây là khai báo của lớp java.util.EnumSet trong java

1     

2

public class EnumMap<K extends Enum<K>,V> extends AbstractMap<K,V>

    implements Serializable, Cloneable 

Trong đó:

  • K: đây là kiểu key để lưu trữ.
  • V: đây là kiểu giá trị được ánh xạ.

Ví dụ EnumMap trong Java

Ví dụ 1:

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    

package vn.viettuts.collection;

 

import java.util.EnumMap;

import java.util.Map;

 

public class EnumMapExample1 {

    /**

     * create an enum

     *

     * @author viettuts.vn

     * @param args

     */

    public enum Days {

        Monday, Tuesday, Wednesday, Thursday, Friday

    };

 

    /**

     * main

     *

     * @author viettuts.vn

     * @param args

     */

    public static void main(String[] args) {

        // create enum map

        EnumMap<Days, String> enumMap = new EnumMap<Days, String>(Days.class);

        // add element to map

        enumMap.put(Days.Monday, "1");

        enumMap.put(Days.Tuesday, "2");

        enumMap.put(Days.Wednesday, "3");

        enumMap.put(Days.Thursday, "4");

        enumMap.put(Days.Friday, "5");

        // show map

        for (Map.Entry<Days, String> map : enumMap.entrySet()) {

            System.out.println(map.getKey() + " " + map.getValue());

        }

    }

}

Kết quả:

 Monday 1
 Tuesday 2
 Wednesday 3
 Thursday 4
 Friday 5
 

Ví dụ 2: EnumMap với lớp Student

Lớp Student.java

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    

package vn.viettuts.collection;

 

/**

 * Student class

 *

 * @author viettuts.vn

 */

public class Student implements Comparable<Student> {

    private String name;

    private int age;

    private String address;

 

    public Student() {

    }

 

    public Student(String name, int age, String address) {

        super();

        this.name = name;

        this.age = age;

        this.address = address;

    }

 

    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 String getAddress() {

        return address;

    }

 

    public void setAddress(String address) {

        this.address = address;

    }

 

    @Override

    public String toString() {

        return "Student@name=" + name + ",age=" + age + ",address=" + address;

    }

}

Lớp EnumMapExample2.java

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     

package vn.viettuts.collection;

 

import java.util.EnumMap;

import java.util.Map;

 

public class EnumMapExample2 {

    /**

     * create an enum

     *

     * @author viettuts.vn

     */

    public enum Key {

        One, Two, Three, Four

    };

 

    /**

     * main

     *

     * @author viettuts.vn

     * @param args

     */

    public static void main(String args[]) {

        // init enumMap

        EnumMap<Key, Student> enumMap = new EnumMap<Key, Student>(Key.class);

        // add elements to enumMap

        enumMap.put(Key.One, new Student("A", 12, "Hanoi"));

        enumMap.put(Key.Two, new Student("B", 13, "Hanoi"));

        enumMap.put(Key.Three, new Student("C", 15, "Hanoi"));

        enumMap.put(Key.Four, new Student("D", 14, "Hanoi"));

        // show enumMap

        for (Map.Entry<Key, Student> map : enumMap.entrySet()) {

            System.out.println(map.getKey() + " " + map.getValue());

        }

    }

}

Kết quả:

 One Student@name=A,age=12,address=Hanoi
 Two Student@name=B,age=13,address=Hanoi
 Three Student@name=C,age=15,address=Hanoi
 Four Student@name=D,age=14,address=Hanoi

Nguồn tin: viettuts.vn