search
HomeJavajavaTutorialEnumMap class in Java

EnumMap class in Java

The java.util.EnumMap class is a Map implementation specifically used to enumerate keys. Here are the important points about EnumMap:

  • All keys in an enum map must come from a specified enum type, either explicitly or implicitly when creating the map.

  • Enumeration mappings are maintained in the natural order of keys.

  • EnumMap is not synchronized. If multiple threads access an enumeration map simultaneously, and at least one thread modifies the map, synchronization should be done externally.

The following is the constructor of the EnumMap class:

##123 table>Example

Let’s see an example-

Live Demonstration

import java.util.EnumMap;
public class Demo {
   // create an enum
   public enum Numbers {
      ONE, TWO, THREE, FOUR, FIVE
   };
   public static void main(String[] args) {
      EnumMap<Numbers, String> map1 = new EnumMap<Numbers, String>(Numbers.class);
      EnumMap<Numbers, String> map2 = new EnumMap<Numbers, String>(Numbers.class);
      // associate values in map1
      map1.put(Numbers.ONE, "1");
      map1.put(Numbers.TWO, "2");
      map1.put(Numbers.THREE, "3");
      map1.put(Numbers.FOUR, "4");
      // print the whole map
      System.out.println("map1:" + map1);
      // clone map1 to map2
      map2 = map1.clone();
      // print map2
      System.out.println("map2:" + map2);
   }
}

Output

map1:{ONE=1, TWO=2, THREE=3, FOUR=4}
map2:{ONE=1, TWO=2, THREE=3, FOUR=4}

Example

Let’s look at another example where we show the count of key-value mappings in a Map:

Live Demonstration

import java.util.*;
public class EnumMapDemo {
   // create an enum
   public enum Numbers {
      ONE, TWO, THREE, FOUR, FIVE
   };
   public static void main(String[] args) {
      EnumMap<Numbers, String> map = new EnumMap<Numbers, String>(Numbers.class);
      // assosiate values in map
      map.put(Numbers.ONE, "1");
      map.put(Numbers.TWO, "2");
      map.put(Numbers.THREE, "3");
      map.put(Numbers.FOUR, "4");
      // print the map
      System.out.println("Map: " + map);
      // print the number of mappings of this map
      System.out.println("Number of mappings:" + map.size());
      // remove value from Numbers.THREE
      map.put(Numbers.FIVE, "5");
      // print the new number of mappings of this map
      System.out.println("Number of mappings:" + map.size());
   }
}

Output

Map: {ONE=1, TWO=2, THREE=3, FOUR=4}
Number of mappings:4
Number of mappings:5

Serial number Constructor and description
EnumMap(Class keyType)This constructor is created using the specified key type An empty enum map.

EnumMap(EnumMap m)The The constructor creates an enum map using the same key types as the specified enum map, Initially contains the same mapping if available.

EnumMap(Map m)This The constructor creates an enum map initialized from the specified map.

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

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)