EnumMap In Java

EnumMap In Java

EnumMap In Java :

  • A flat array of the Map’s values indexed by the ordinals of their keys.
  • Java EnumMap class is the Map implementation for enum keys. It inherits Enum and AbstractMap classes.
  • EnumMap keep order of their collection, which is same as the order of enum constants.
  • EnumMap is not synchronized and it has to be synchronized manually before using it in a concurrent or multi-threaded environment.
  • It is faster than Hashmap as it has already limited keys which is predicted by enum constants.
  • EnumMap are efficient, compact implementations of the Map interfaces. Keys of EnumMap must be coming from single entry.
  • Iterators of EnumMap are fail-fast Iterator , much like of ConcurrentHashMap Iterates over entries in a predictable order (the declaration order of the key type’s enum constants).
  • Rejects null keys.
  • All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created.
  • enum maps are maintained in the natural order of their keys.
  • EnumMap class
public class EnumMap<K extends Enum<K>,V> extends AbstractMap<K,V> implements Serializable, Cloneable  
1 void clear() It is used to remove all mappings from this map.
2 boolean containsKey(Object key) This method return true if this map contains a mapping for the specified key.
3 boolean containsValue(Object value) This method return true if this map maps one or more keys to the specified value.
4 boolean equals(Object o) It is used to compare the specified object with this map for equality.
5 V get(Object key) This method returns the value to which the specified key is mapped.
6 V put(K key, V value) It is used to associate the specified value with the specified key in this map.
7 V remove(Object key) It is used to remove the mapping for this key.
8 Collection<V> values() It is used to return a Collection view of the values contained in this map.
9 int size() It is used to return the number of key-value mappings in this map.
  • Factory method
Map<DayOfWeek, String> dayNames =
    EnumMap.of(
        DayOfWeek.MONDAY,    "lunes",
        DayOfWeek.TUESDAY,   "martes");
import java.util.*;

public class EnumDay {
    // create an enum
    public enum Days {
        Monday, Tuesday, Wednesday, Thursday
    };

    public static void main(String[] args) { 
        EnumMap<Days, String> map = new EnumMap<Days, String>(Days.class);
        map.put(Days.Monday, "1");
        map.put(Days.Tuesday, "2");
        map.put(Days.Wednesday, "3");
        map.put(Days.Thursday, "4");
        //Print
        for (Map.Entry m : map.entrySet()) {
            System.out.println(m.getKey() + " " + m.getValue());
        }
    }
}
import java.util.EnumMap;
import java.util.Iterator;

public class EnumMapExample{

    public enum STATE{
        NEW, RUNNING, WAITING, FINISHED;
    }

    public static void main(String args[]) {

        // Java EnumMap Example 1: creating EnumMap in java with key as enum type STATE
        EnumMap<STATE, String> stateMap = new EnumMap<STATE, String>(STATE.class);

        // Java EnumMap Example 2:
        //putting values inside EnumMap in Java
        //we are inserting Enum keys on different order than their natural order
        stateMap.put(STATE.RUNNING, "Program is running");
        stateMap.put(STATE.WAITING, "Program is waiting");
        stateMap.put(STATE.NEW, "Program has just created");
        stateMap.put(STATE.FINISHED, "Program has finished");

        // Java EnumMap Example 3:
        //printing size of EnumMap in java
        System.out.println("Size of EnumMap in java: " + stateMap.size());

        // Java EnumMap Example 5:
        //printing Java EnumMap , should print EnumMap in natural order 
        //of enum keys (order on which they are declared)
        System.out.println("EnumMap: " + stateMap);

        // Java EnumMap Example 5:
        //retrieving value from EnumMap in java
        System.out.println("EnumMap key : " + STATE.NEW +" value: " + stateMap.get(STATE.NEW));

        // Java EnumMap Example 6:
        //Iterating over Java EnumMap
        Iterator<STATE> enumKeySet = stateMap.keySet().iterator();
        while(enumKeySet.hasNext()){
            STATE currentState = enumKeySet.next();
            System.out.println("key : " + currentState + " value : " + stateMap.get(currentState));
        }

        //Java EnumMap Example 7: checking if EnumMap contains a particular key
        System.out.println("Does stateMap has :" + STATE.NEW + " : "
                +  stateMap.containsKey(STATE.NEW));

        //Java EnumMap Example 8: checking if EnumMap contains a particular value
        System.out.println("Does stateMap has :" + STATE.NEW + " : " + stateMap.containsValue(null));

    }
}
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
 
enum Direction {
  // Enum types
  EAST(0), WEST(180), NORTH(90), SOUTH(270);
  // Constructor
  private Direction(final int angle) {
    this.angle = angle;
  }
 
  // Internal state
  private int angle;
 
  public int getAngle() {
    return angle;
  }
 
  // Lookup table
  private static final Map lookup = new HashMap();
 
  // Populate the lookup table on loading time
  static {
    for (Direction s : EnumSet.allOf(Direction.class))
      lookup.put(s.getAngle(), s);
  }
 
  // This method can be used for reverse lookup purpose
  public static Direction get(int angle) {
    return lookup.get(angle);
  }
}
public class Test {
  public static void main(String[] args) {
    // Angle lookup
    System.out.println(Direction.NORTH.getAngle());
    // Reverse lookup by angle
    System.out.println(Direction.get(90));
  }
}
import java.util.EnumMap;
public class EnumMapDemo {
    public static void main(String[] args) {
        EnumMap<TestEnum, String> enumMap = new EnumMap<TestEnum, String>(TestEnum.class);
        enumMap.put(TestEnum.A, "AAA");
        enumMap.put(TestEnum.B, "BBB");
        enumMap.put(TestEnum.C, "CCC");
        enumMap.put(TestEnum.D, "DDD");
        //iterate over key
        System.out.println("---iterate over key---");
        for(TestEnum e: enumMap.keySet()){
            System.out.println(e +" "+ e.getCode() );
        }
       //iterate over value
        System.out.println("---iterate over value---");
        for(String s: enumMap.values()){
            System.out.println(s);
        }
    }
}
enum TestEnum {
    A(1), B(2), C(3), D(4);
    private int code;
    private TestEnum(int c) {
        code = c;
    }
    public int getCode() {
        return code;
    }
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in