Home >Java >javaTutorial >Java EnumMap

Java EnumMap

王林
王林Original
2024-08-30 15:47:49758browse

A Java Collection Framework member known as EnumMap, which extends AbstractMap, is a specific implementation for the interface map, mainly for enumeration types. In addition to that, several other features are also present for EnumMap.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

It includes:

  • It is not synchronized.
  • Faster; Compared to this, HashMap is slower.
  • It is kept in the key’s natural order.
  • High performance.
  • Null keys are not allowed. That is, a NullPointerException will be thrown when null keys are present.
  • Null values can be present.
  • As keys, enum elements will be used.

More details on Java EnumMap will be discussed in the following sections.

Syntax

Java EnumMap can be declared using the below syntax.

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

Constructors in Java EnumMap

The following are the constructors that are commonly used in Java EnumMap:

  • EnumMap(Class<K> keyType): An empty enum map will be created with the mentioned keyType.
  • EnumMap(EnumMap<K,? extends V> m): An enum map will be created with the keyType the same as that of the mentioned enum map if any initial mappings are present.
  • EnumMap(Map<K,? extends V> m): An enum map will be created from the mentioned map.

Methods in Java EnumMap

Now, let us see some of the commonly used methods of Java EnumMap.

  • clear(): Every mapping available in the map will be removed on calling this method.
  • clone(): A shallow copy will be returned for the enum map mentioned.
  • containsKey(Objectk): True will be returned if the map has a mapping for the mentioned key k.
  • containsValue(Objectv): True will be returned if the map maps 1 or more than 1 key to the mentioned value v.
  • entrySet(): A set view will be returned for the mappings that are available on the map.
  • equals(Objectob): Map and the mentioned object ob will be compared for equality.
  • get(Objectk): Value will be returned if the map has a mapping for the mentioned key k. If there is no value for the key, null will be returned.
  • hashCode(): Hash code will be returned for the map.
  • keySet(): A set view will be returned for the keys that are present on the map.
  • put(Kkey, V value): The value v will be associated with the key K on the map.
  • putAll(MapK,? extends V> m): Each and every mapping from the given map will be copied to this map.
  • remove(Objectk): Mapping will be removed for the key k that is mentioned.
  • size(): Count of key-value mappings available in the map will be returned.
  • values(): A collection view will be returned for the values available on the map.

Examples to Implement Java EnumMap

In order to understand more about the Java Enum Map, let us implement the above-mentioned methods in some programs.

Example #1

Sample program to create an enum map and copy the elements to another 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 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);
}
}

Output:

Java EnumMap

Explanation to the above program: In the above program, two enum maps are created. The first map is created with 2 elements, and the second map is created by copying the elements of the first map. In addition to that, an extra element is also added to the second map. These are done with the help of put() and putAll() methods.

Example #2

Sample program to create an enum map and get the keys and values separately.

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 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());
}
}

Output:

Java EnumMap

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 EnumMap

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.

The above is the detailed content of Java EnumMap. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Hashtable in JavaNext article:Hashtable in Java