Java中的EnumMap类

EnumMap是 地图界面 对于 枚举类型 .它扩展了AbstractMap并实现了 地图 Java中的接口。它属于 JAVAutil包 .EnumMap的几个重要功能如下:

null
  • EnumMap类是 Java集合框架 &不同步。
  • EnumMap是一个有序集合,它们按键的自然顺序进行维护(键的自然顺序是指在enum类型中声明枚举常量的顺序)
  • 这是一个高性能的映射实现,比 哈希图 .
  • 每个EnumMap实例的所有键必须是单个 枚举 类型
  • EnumMap不允许空键和抛出 空指针异常 当我们试图插入空密钥时。
  • 集合视图返回的迭代器弱一致:它们永远不会抛出 ConcurrentModificationException 它们可能会也可能不会显示在迭代过程中对地图进行的任何修改的效果。
  • EnumMap在内部表示为数组。这种表示非常紧凑和高效。

语法: 公告

public class EnumMap<K extends Enum<K>,​V> extends AbstractMap<K,​V> implements Serializable, Cloneable

参数:

  • 关键对象类型
  • 值对象类型

K 必须延长 枚举 ,强制要求钥匙必须是指定的 枚举 类型

枚举映射层次结构

EnumMap-in-Java

枚举映射的构造函数

  1. EnumMap(类键类型): 构造函数用于使用指定的 钥匙类型 .
  2. EnumMap(EnumMap m): 构造函数用于创建与指定枚举映射具有相同密钥类型的枚举映射,初始映射与枚举映射相同
  3. EnumMap(地图m): 构造函数用于从参数中指定的映射创建带有初始化的枚举映射。

实例

JAVA

// Java Program to illustrate Working of EnumMap class
// and its functions
// Importing EnumMap class
import java.util.EnumMap;
// Main class
public class EnumMapExample {
// Enum
public enum GFG {
CODE,
CONTRIBUTE,
QUIZ,
MCQ;
}
// Main driver method
public static void main(String args[])
{
// Java EnumMap
// Creating an empty EnumMap with key
// as enum type state
EnumMap<GFG, String> gfgMap
= new EnumMap<GFG, String>(GFG. class );
// Putting values inside EnumMap in Java
// Inserting Enum keys different from
// their natural order
gfgMap.put(GFG.CODE, "Start Coding with gfg" );
gfgMap.put(GFG.CONTRIBUTE, "Contribute for others" );
gfgMap.put(GFG.QUIZ, "Practice Quizes" );
gfgMap.put(GFG.MCQ, "Test Speed with Mcqs" );
// Printing size of EnumMap
System.out.println( "Size of EnumMap in java: "
+ gfgMap.size());
// Printing Java EnumMap
// Print EnumMap in natural order
// of enum keys (order on which they are declared)
System.out.println( "EnumMap: " + gfgMap);
// Retrieving value from EnumMap
System.out.println( "Key : " + GFG.CODE + " Value: "
+ gfgMap.get(GFG.CODE));
// Checking if EnumMap contains a particular key
System.out.println(
"Does gfgMap has " + GFG.CONTRIBUTE + ": "
+ gfgMap.containsKey(GFG.CONTRIBUTE));
// Checking if EnumMap contains a particular value
System.out.println(
"Does gfgMap has :" + GFG.QUIZ + " : "
+ gfgMap.containsValue( "Practice Quizes" ));
System.out.println( "Does gfgMap has :" + GFG.QUIZ
+ " : "
+ gfgMap.containsValue( null ));
}
}


输出

Size of EnumMap in java: 4EnumMap: {CODE=Start Coding with gfg, CONTRIBUTE=Contribute for others, QUIZ=Practice Quizes, MCQ=Test Speed with Mcqs}Key : CODE Value: Start Coding with gfgDoes gfgMap has CONTRIBUTE: trueDoes gfgMap has :QUIZ : trueDoes gfgMap has :QUIZ : false

EnumMap上的基本操作

操作1: 添加元素

为了向EnumMap添加元素,我们可以使用put()或putAll()方法,如下所示。

JAVA

// Java Program to Add Elements to the EnumMap
// Importing EnumMap class
import java.util.EnumMap;
// Main class
// AddingElementsToEnumMap
class GFG {
enum Color { RED, GREEN, BLUE, WHITE }
public static void main(String[] args)
{
// Creating an EnumMap of the Color enum
EnumMap<Color, Integer> colors1
= new EnumMap<>(Color. class );
// Insert elements in Map
// using put() method
colors1.put(Color.RED, 1 );
colors1.put(Color.GREEN, 2 );
// Printing mappings to the console
System.out.println( "EnumMap colors1: " + colors1);
// Creating an EnumMap of the Color Enum
EnumMap<Color, Integer> colors2
= new EnumMap<>(Color. class );
// Adding elements using the putAll() method
colors2.putAll(colors1);
colors2.put(Color.BLUE, 3 );
// Printing mappings to the console
System.out.println( "EnumMap colors2: " + colors2);
}
}


输出

EnumMap colors1: {RED=1, GREEN=2}EnumMap colors2: {RED=1, GREEN=2, BLUE=3}

操作2: 访问元素

我们可以使用entrySet()、keySet()、values()、get()访问EnumMap的元素。下面的例子解释了这些方法。

JAVA

// Java Program to Access the Elements of EnumMap
// Importing required classes
import java.util.EnumMap;
// Main class
// AccessElementsOfEnumMap
class GFG {
// Enum
enum Color { RED, GREEN, BLUE, WHITE }
// Main driver method
public static void main(String[] args)
{
// Creating an EnumMap of the Color enum
EnumMap<Color, Integer> colors
= new EnumMap<>(Color. class );
// Inserting elements using put() method
colors.put(Color.RED, 1 );
colors.put(Color.GREEN, 2 );
colors.put(Color.BLUE, 3 );
colors.put(Color.WHITE, 4 );
System.out.println( "EnumMap colors : " + colors);
// Using the entrySet() method
System.out.println( "Key/Value mappings: "
+ colors.entrySet());
// Using the keySet() method
System.out.println( "Keys: " + colors.keySet());
// Using the values() method
System.out.println( "Values: " + colors.values());
// Using the get() method
System.out.println( "Value of RED : "
+ colors.get(Color.RED));
}
}


输出

EnumMap colors : {RED=1, GREEN=2, BLUE=3, WHITE=4}Key/Value mappings: [RED=1, GREEN=2, BLUE=3, WHITE=4]Keys: [RED, GREEN, BLUE, WHITE]Values: [1, 2, 3, 4]Value of RED : 1

操作3: 移除元素

为了删除元素,EnumMap提供了remove()方法的两种变体。

实例

JAVA

// Java program to Remove Elements of EnumMap
// Importing EnumMap class
import java.util.EnumMap;
// Main class
class GFG {
// Enum
enum Color {
// Custom elements
RED,
GREEN,
BLUE,
WHITE
}
// Main driver method
public static void main(String[] args)
{
// Creating an EnumMap of the Color enum
EnumMap<Color, Integer> colors
= new EnumMap<>(Color. class );
// Inserting elements in the Map
// using put() method
colors.put(Color.RED, 1 );
colors.put(Color.GREEN, 2 );
colors.put(Color.BLUE, 3 );
colors.put(Color.WHITE, 4 );
// Printing colors in the EnumMap
System.out.println( "EnumMap colors : " + colors);
// Removing a mapping
// using remove() Method
int value = colors.remove(Color.WHITE);
// Displaying the removed value
System.out.println( "Removed Value: " + value);
// Removing specific color and storing boolean
// if removed or not
boolean result = colors.remove(Color.RED, 1 );
// Printing the boolean result whether removed or
// not
System.out.println( "Is the entry {RED=1} removed? "
+ result);
// Printing the updated Map to the console
System.out.println( "Updated EnumMap: " + colors);
}
}


输出

EnumMap colors : {RED=1, GREEN=2, BLUE=3, WHITE=4}Removed Value: 4Is the entry {RED=1} removed? trueUpdated EnumMap: {GREEN=2, BLUE=3}

操作4: 更换元件

地图界面 提供replace()方法的三种变体,以更改EnumMap的映射。

实例

JAVA

// Java Program to Replace Elements of EnumMap
// Importing required classes
import java.util.EnumMap;
// Main class
class GFG {
// Enum
enum Color {
RED,
GREEN,
BLUE,
WHITE
}
// Main driver method
public static void main(String[] args)
{
// Creating an EnumMap of the Color enum
EnumMap<Color, Integer> colors
= new EnumMap<>(Color. class );
// Inserting elements to Map
// using put() method
colors.put(Color.RED, 1 );
colors.put(Color.GREEN, 2 );
colors.put(Color.BLUE, 3 );
colors.put(Color.WHITE, 4 );
// Printing all elements inside above Map
System.out.println( "EnumMap colors " + colors);
// Replacing certain elements depicting colors
// using the replace() method
colors.replace(Color.RED, 11 );
colors.replace(Color.GREEN, 2 , 12 );
// Printing the updated elements (colors)
System.out.println( "EnumMap using replace(): "
+ colors);
// Replacing all colors using the replaceAll()
// method
colors.replaceAll((key, oldValue) -> oldValue + 3 );
// Printing the elements of above Map
System.out.println( "EnumMap using replaceAll(): "
+ colors);
}
}


输出

EnumMap colors {RED=1, GREEN=2, BLUE=3, WHITE=4}EnumMap using replace(): {RED=11, GREEN=12, BLUE=3, WHITE=4}EnumMap using replaceAll(): {RED=14, GREEN=15, BLUE=6, WHITE=7}

同步枚举映射

EnumMap的实现不同步。这意味着,如果多个线程同时访问一个树集,并且至少有一个线程修改了该树集,那么它必须在外部同步。这通常是通过使用 收藏课 。这最好在创建时完成,以防止意外的非同步访问。

 Map<EnumKey, V> m = Collections.synchronizedMap(new EnumMap<EnumKey, V>(...));

枚举映射方法

  • K–关键对象的类型
  • V–值对象的类型

方法

执行的动作

清除() 删除此映射中的所有映射。
克隆() 返回此枚举映射的浅层副本。
康纳斯基​(对象键) 如果此映射包含指定键的映射,则返回true。
包含价值​(对象值) 如果此映射将一个或多个键映射到指定值,则返回true。
入口集() 返回此映射中包含的映射的集合视图。
等于​(对象o) 将指定的对象与此映射进行相等性比较。
收到​(对象键) 返回指定键映射到的值,如果此映射不包含该键的映射,则返回null。
hashCode() 返回此映射的哈希代码值。
键集() 返回此映射中包含的键的集合视图。
放​(K键,V值) 将指定的值与此映射中的指定键相关联。
普塔尔​(地图m) 将指定映射中的所有映射复制到此映射。
去除​(对象键) 从映射中删除此键的映射(如果存在)。
大小() 返回此映射中的键值映射数。
价值观() 返回此映射中包含的值的集合视图。

AbstractMap类中声明的方法

方法

描述

isEmpty() 如果此映射不包含键值映射,则返回true。
toString() 返回此映射的字符串表示形式。

在java接口中声明的方法。util。地图

方法

描述符

计算​(K键,双功能重新映射功能) 尝试计算指定键及其当前映射值的映射(如果没有当前映射,则为null)。
计算机​(K键,功能映射功能) 如果指定的键尚未与值关联(或映射为null),则尝试使用给定的映射函数计算其值,并将其输入该映射,除非为null。
计算机呈现​(K键,双功能重新映射功能) 如果指定键的值存在且非空,则会尝试在给定键及其当前映射值的情况下计算新映射。
弗雷奇​(双消费者动作) 对该映射中的每个条目执行给定操作,直到所有条目都已处理或该操作引发异常为止。
getOrDefault​(对象键,V默认值) 返回指定键映射到的值,如果此映射不包含该键的映射,则返回defaultValue。
合并​(K键,V值,双功能重新映射功能) 如果指定的键尚未与值关联或与null关联,则将其与给定的非null值关联。
putIfAbsent​(K键,V值) 如果指定的键尚未与值关联(或映射为null),则将其与给定值关联并返回null,否则返回当前值。
去除​(对象键、对象值) 仅当指定项当前映射到指定值时,才删除该项。
代替​(K键,V值) 仅当指定项当前映射到某个值时,才替换该项。
代替​(K键,V旧值,V新值) 仅当当前映射到指定值时,才替换指定键的项。
替补队员​(双功能功能) 将每个条目的值替换为对该条目调用给定函数的结果,直到处理完所有条目或函数引发异常为止。

EnumMap vs EnumSet

所有物

枚举图

枚举集

内部代表 EnumMap在内部表示为数组。这种表示方法简洁高效。 枚举集在内部表示为位向量或位序列。
允许空元素吗? 不允许使用空键,但允许使用空值。 不允许使用空元素。
这个 抽象类?
实例化 因为EnumMap不是一个抽象类,所以可以使用新的操作符实例化它。 它是一个抽象类,没有构造函数。枚举集是使用其预定义的方法创建的,如allOf()、noneOf()、of()等。
实施 EnumMap是一个专门的映射实现,用于枚举类型键。 EnumSet是一种专门用于枚举类型的集合实现。

本文由 普拉蒂克·阿加瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgek的主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享