A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/util/java_util_enummap.htm below:

Java EnumMap Class

Java EnumMap Class Introduction

The Java EnumMap class is a specialized Map implementation for use with enum keys.Following are the important points about EnumMap −

Class declaration

Following is the declaration for java.util.EnumMap class −

public class EnumMap<K extends Enum<K>,V>
   extends AbstractMap<K,V>
   implements Serializable, Cloneable
Class constructors Sr.No. Constructor & Description 1

EnumMap(Class<K> keyType)

This constructor creates an empty enum map with the specified key type.

2

EnumMap(EnumMap<K,? extends V> m)

This constructor creates an enum map with the same key type as the specified enum map, initially containing the same mappings (if any).

3

EnumMap(Map<K,? extends V> m)

This constructor creates an enum map initialized from the specified map.

Class methods Methods inherited

This class inherits methods from the following classes −

Adding a Key-Value to an EnumMap Of Enum, Integer Pair Example

The following example shows the usage of Java EnumMap put(K,V) method to put a value in the EnumMap instance. We've created a enum Numbers. Then EnumMap is created of enum Numbers and Integer. Few entries are added using put(K,V) and enumMap is printed. Using put() method again, a value of enumMap is replaced and map is printed again.

package com.tutorialspoint;

import java.util.EnumMap;

public class EnumMapDemo {
   
   // create an enum
   public enum Numbers{ONE, TWO, THREE, FOUR, FIVE}; 

   public static void main(String[] args) {
      
      EnumMap<Numbers,Integer> map = 
         new EnumMap<>(Numbers.class);

      // associate values in map
      map.put(Numbers.ONE, 1);
      map.put(Numbers.TWO, 2);
      map.put(Numbers.THREE,3);

      // print the whole map
      System.out.println(map); 

      map.put(Numbers.THREE, 4);
	  
      // print the updated map
      System.out.println(map);
   }
}

Let us compile and run the above program, this will produce the following result −

{ONE=1, TWO=2, THREE=3}
{ONE=1, TWO=2, THREE=4}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4