首页  >  文章  >  Java  >  Java 枚举映射

Java 枚举映射

王林
王林原创
2024-08-30 15:47:49713浏览

Java Collection Framework 的一个成员 EnumMap,它扩展了 AbstractMap,是接口 Map 的具体实现,主要针对枚举类型。除此之外,EnumMap 还提供其他几个功能。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

它包括:

  • 未同步。
  • 更快;相比之下,HashMap 更慢。
  • 它保持密钥的自然顺序。
  • 高性能。
  • 不允许使用空键。也就是说,当存在 null 键时将抛出 NullPointerException。
  • 可以存在空值。
  • 将使用枚举元素作为键。

有关 Java EnumMap 的更多详细信息将在以下部分中讨论。

语法

Java EnumMap 可以使用以下语法声明。

public class EnumMap<Key extends Enum<Key>,Value> extends AbstractMap<Key,Value> implements Serializable, Cloneable

Java EnumMap 中的构造函数

以下是Java EnumMap中常用的构造函数:

  • EnumMapK>keyType):将使用提到的 keyType 创建一个空的枚举映射。
  • EnumMap(EnumMapK,? 扩展V> m): 如果存在任何初始映射,将创建一个枚举映射,其 keyType 与所提到的枚举映射相同。
  • EnumMap(MapK,? 延伸 V> m): 将从上述映射创建一个枚举映射。

Java EnumMap 中的方法

现在,让我们看看Java EnumMap的一些常用方法。

  • clear():调用此方法时,地图中可用的每个映射都将被删除。
  • clone(): 将为提到的枚举映射返回一个浅拷贝。
  • containsKey(Objectk): 如果地图具有上述键 k 的映射,则返回 True。
  • containsValue(Objectv): 如果映射将 1 个或多个键映射到提到的值 v,则返回 True。
  • entrySet():将为地图上可用的映射返回集合视图。
  • equals(Objectob): 将比较 Map 和提到的对象 ob 是否相等。
  • get(Objectk):如果映射具有上述键 k 的映射,则将返回值。如果键没有值,则返回 null。
  • hashCode():将为地图返回哈希代码。
  • keySet():将为地图上出现的键返回一个集合视图。
  • put(Kkey, V value): 值 v 将与地图上的K键。
  • putAll(地图 延伸 K,? 延伸 V>  m): 给定地图中的每个映射都将复制到此地图。
  • remove(Objectk):将删除提到的键 k 的映射。
  • size(): 将返回映射中可用的键值映射计数。
  • values():将返回地图上可用值的集合视图。

实现 Java EnumMap 的示例

为了更多地了解Java Enum Map,让我们在一些程序中实现上述方法。

示例#1

用于创建枚举映射并将元素复制到另一个枚举映射的示例程序。

代码:

import java.util.EnumMap;
class JavaEnumMapExample {
enum fruits {
APPLE, ORANGE, GRAPES, KIWI
}
public static void main(String[] args) {
// EnumMap creation of the fruits enum
EnumMap<fruits, Integer> fr = new EnumMap<>(fruits.class);
// add key-value to the mapping using the method put()
fr.put(fruits.APPLE, 2);
fr.put(fruits.ORANGE, 5);
System.out.println("The key-value pairs in EnumMap 1 is :  " + fr);
//create another enum map fru
EnumMap<fruits, Integer> fru = new EnumMap<>(fruits.class);
// copy all the elements from first enum map to this using the methodputAll()
fru.putAll(fr);
fru.put(fruits.GRAPES, 3);
System.out.println("The key-value pairs in EnumMap 2 is : " + fru);
}
}

输出:

Java 枚举映射

上述程序说明:在上面的程序中,创建了两个枚举映射。第一个映射是用 2 个元素创建的,第二个映射是通过复制第一个映射的元素创建的。除此之外,第二张地图上还添加了一个额外的元素。这些是在 put() 和 putAll() 方法的帮助下完成的。

示例#2

创建枚举映射并分别获取键和值的示例程序。

代码:

import java.util.EnumMap;
class JavaEnumMapExample {
enum fruits {
APPLE, ORANGE, GRAPES, KIWI
}
public static void main(String[] args) {
// EnumMap creation of the fruits enum
EnumMap<fruits, Integer> fr = new EnumMap<>(fruits.class);
// add key-value to the mapping using the method put()
fr.put(fruits.APPLE, 2);
fr.put(fruits.ORANGE, 5);
System.out.println("The key-value pairs in EnumMap 1 is :  " + fr);
// print all the keys in the enum map using the method keySet()
System.out.println("The keys in enum map 1 are : " + fr.keySet());
// print all the values in the enum map using the method values()
System.out.println("The values in enum map 1 are : " + fr.values());
//create another enum map fru
EnumMap<fruits, Integer> fru = new EnumMap<>(fruits.class);
// copy all the elements from first enum map to this using the methodputAll()
fru.putAll(fr);
fru.put(fruits.GRAPES, 3);
System.out.println("The key-value pairs in EnumMap 2 is : " + fru);
// print all the keys in the enum map using the method keySet()
System.out.println("The keys in enum map 2 are : " + fru.keySet());
// print all the values in the enum map using the method values()
System.out.println("The values in enum map 2 are : " + fru.values());
}
}

输出:

Java 枚举映射

Explanation to the above program: Similar to the first program, two enum maps are available here. This program displays the maps’ keys and values independently using the methods keyset() and values(), respectively.

Example #3

Sample program to remove an element from the enum map

Code:

import java.util.EnumMap;
class JavaEnumMapExample {
enum fruits {
APPLE, ORANGE, GRAPES, KIWI
}
public static void main(String[] args) {
// EnumMap creation of the fruits enum
EnumMap<fruits, Integer> fr = new EnumMap<>(fruits.class);
// add key-value to the mapping using the method put()
fr.put(fruits.APPLE, 2);
fr.put(fruits.ORANGE, 5);
System.out.println("The key-value pairs in EnumMap :  " + fr);
// remove an element using the method remove()
int val = fr.remove(fruits.APPLE);
System.out.println("Removed Value: " + val);
System.out.println("The key-value pairs in EnumMap after removing apple :  " + fr);
}
}

Output:

Java 枚举映射

Explanation to the above program: In this program, an element is removed from the map using the remove() method and the resultant enum map is printed in the next step.

Conclusion

A detailed explanation of all the aspects such as declaration, methods, constructors of Java EnumMap is discussed in this document in detail.

以上是Java 枚举映射的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Hashtable in Java下一篇:Java LinkedHashMap