📜  Java EnumMap类

📅  最后修改于: 2020-10-13 00:33:06             🧑  作者: Mango

Java EnumMap类

Java EnumMap类是枚举键的专用Map实现。它继承了Enum和AbstractMap类。

EnumMap类层次结构

EnumMap类的层次结构在下图中给出。

EnumMap类声明

我们来看一下java.util.EnumMap类的声明。

public class EnumMap,V> extends AbstractMap implements Serializable, Cloneable

EnumMap类参数

让我们看一下java.util.EnumMap类的参数。

  • K:这是此地图维护的键的类型。
  • V:这是映射值的类型。

Java EnumMap类的构造方法

Constructor Description
EnumMap(Class keyType) It is used to create an empty enum map with the specified key type.
EnumMap(EnumMap m) It is used to create an enum map with the same key type as the specified enum map.
EnumMap(Map m) It is used to create an enum map initialized from the specified map.

Java EnumMap类的方法

SN Method Description
1 clear() It is used to clear all the mapping from the map.
2 clone() It is used to copy the mapped value of one map to another map.
3 containsKey() It is used to check whether a specified key is present in this map or not.
4 containsValue() It is used to check whether one or more key is associated with a given value or not.
5 entrySet() It is used to create a set of elements contained in the EnumMap.
6 equals() It is used to compare two maps for equality.
7 get() It is used to get the mapped value of the specified key.
8 hashCode() It is used to get the hashcode value of the EnumMap.
9 keySet() It is used to get the set view of the keys contained in the map.
10 size() It is used to get the size of the EnumMap.
11 Values() It is used to create a collection view of the values contained in this map.
12 put() It is used to associate the given value with the given key in this EnumMap.
13 putAll() It is used to copy all the mappings from one EnumMap to a new EnumMap.
14 remove() It is used to remove the mapping for the given key from EnumMap if the given key is present.

Java EnumMap示例

import java.util.*;
public class EnumMapExample {
   // create an enum
   public enum Days {
   Monday, Tuesday, Wednesday, Thursday
   };
   public static void main(String[] args) {
   //create and populate enum map
   EnumMap map = new EnumMap(Days.class);
   map.put(Days.Monday, "1");
   map.put(Days.Tuesday, "2");
   map.put(Days.Wednesday, "3");
   map.put(Days.Thursday, "4");
   // print the map
   for(Map.Entry m:map.entrySet()){  
   System.out.println(m.getKey()+" "+m.getValue());  
  } 
   }
}

输出:

Monday 1
Tuesday 2
Wednesday 3
Thursday 4

Java EnumMap示例:书籍

import java.util.*;  
class Book {  
int id;  
String name,author,publisher;  
int quantity;  
public Book(int id, String name, String author, String publisher, int quantity) {  
    this.id = id;  
    this.name = name;  
    this.author = author;  
    this.publisher = publisher;  
    this.quantity = quantity;  
}  
}  
public class EnumMapExample { 
// Creating enum
public enum Key{
   One, Two, Three
   };
public static void main(String[] args) {  
EnumMap map = new EnumMap(Key.class);
    // Creating Books  
    Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);  
    Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);  
    Book b3=new Book(103,"Operating System","Galvin","Wiley",6);  
    // Adding Books to Map 
       map.put(Key.One, b1);
   map.put(Key.Two, b2);
   map.put(Key.Three, b3);
    // Traversing EnumMap
   for(Map.Entry entry:map.entrySet()){    
        Book b=entry.getValue();  
        System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);   
    }     
}  
}  

输出:

101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6