What Is Enummap Inwards Coffee – Illustration Tutorial

Advertisement

Masukkan script iklan 970x90px

What Is Enummap Inwards Coffee – Illustration Tutorial

Sabtu, 18 Juli 2020

What is EnumMap inwards Java
EnumMap inwards Java is added on JDK  5 unloose along amongst other of import features like  Autoboxing, varargs in addition to Generics. EnumMap is specialized Map implementation designed in addition to optimized for using Java Enum equally key. Since enum tin stand upwards for a type (like cast or interface)  in Java in addition to it tin also override equals() in addition to hashCode() , It can survive used within HashMap or whatever other collection but using  EnumMap brings implementation specific benefits which are done for enum keys, In curt EnumMap is optimized Map implementation entirely for enum keys . As per Javadoc Enum is implemented using Arrays in addition to mutual operations final result inwards constant time. So if you lot are thinking of an high-performance Map, EnumMap could survive a decent selection for enumeration data. We choose already seen many examples of Java enum inwards our article 10 Examples of enum inwards Java  and using Enum equally thread-safe Singleton. In this Java tutorial, nosotros volition come across uncomplicated examples of using EnumMap in Java. 

On related banking concern complaint Difference betwixt EnumMap in addition to HashMap is too getting pop equally 1 of advanced Java collection interviews questions in addition to most of Java IDE similar Eclipse in addition to Netbeans volition too propose to role EnumMap if you lot role Enum keys along amongst HashMap equally business office of at that topographic point code improvement suggestion.


Important points of EnumMap in Java:
 unloose along amongst other of import features similar What is EnumMap inwards Java – Example TutorialHere is few of import points to recall almost EnumMap inwards Java which is too useful spell using EnumMap inwards code to avoid whatever compile fourth dimension or logical errors :


1. All keys used inwards EnumMap must survive  from same Enum type which is specified spell creating EnumMap in Java. For illustration if you lot tin non role unlike enum instances from ii unlike enum.

2. EnumMap is ordered collection and they are maintained inwards the natural fellowship of their keys( natural fellowship of keys means  the fellowship on which enum constant are declared within enum type ). you lot tin verify this spell Iterating over an EnumMap in Java.

3. Iterators of EnumMap are fail-fast Iterator , much similar of ConcurrentHashMap in addition to doesn't throw ConcurrentModificationException in addition to may non present upshot of whatever change on EnumMap during Iteration process.

4. You tin non insert null keys within EnumMap inwards Java.  EnumMap doesn't allow null cardinal in addition to throw NullPointerException, at same fourth dimension zilch values are permitted.

5. EnumMap is non synchronized in addition to it has to survive synchronized manually earlier using it inwards a concurrent or multi-threaded environment. similar synchronized Map inwards Java  you tin too brand EnumMap synchronized past times using Collections.synchronizedMap() method in addition to equally per javadoc this should survive done spell creating EnumMap in coffee to avoid accidental non synchronized access.

6. EnumMap is probable orbit amend performance than HashMap inwards Java. So prefer EnumMap if you lot are going to role enum keys.

How to role EnumMap inwards Java – EnumMap Example

In this department nosotros volition come across How to role EnumMap inwards Java amongst uncomplicated examples similar creating EnumMap, putting objects into EnumMap, getting objects from EnumMap,  finding size of EnumMap, Iterating over EnumMap, printing EnumMap inwards console , checking if EnumMap contains a item cardinal in addition to value or non etc. All of these operations are similar to other Map implementation similar HashMap and doesn’t quest particular noesis but It’s practiced to recall all points specific to EnumMap equally discussed inwards previous department to avoid whatever error.


import java.util.EnumMap;
import java.util.Iterator;

/**
 * Java plan to demonstrate How to role EnumMap inwards Java
 * If Key Object is Enum than it’s best to EnumMap to teach amend performance.
 * Most of IDE similar Netbeans in addition to Eclipse propose you lot to role EnumMap instead of HashMap
 * or whatever other Map implementation when cardinal object is Enum.
 *
 * @author
 */


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

    public static void main(String args[]) {
     
        // Java EnumMap Example 1: creating EnumMap inwards coffee amongst cardinal equally enum type STATE
        EnumMap<STATE, String> stateMap = new EnumMap<STATE, String>(STATE.class);
     
        // Java EnumMap Example 2:
        //putting values within EnumMap inwards Java
        //we are inserting Enum keys on unlike fellowship than their natural order
        stateMap.put(STATE.RUNNING, "Program is running");
        stateMap.put(STATE.WAITING, "Program is waiting");
        stateMap.put(STATE.NEW, "Program has only created");
        stateMap.put(STATE.FINISHED, "Program has finished");
     
        // Java EnumMap Example 3:
        //printing size of EnumMap inwards java
        System.out.println("Size of EnumMap inwards java: " + stateMap.size());
     
        // Java EnumMap Example 5:
        //printing Java EnumMap , should impress EnumMap inwards natural fellowship
        //of enum keys (order on which they are declared)
        System.out.println("EnumMap: " + stateMap);
     
        // Java EnumMap Example 5:
        //retrieving value from EnumMap inwards java
        System.out.println("EnumMap cardinal : " + 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 item key
        System.out.println("Does stateMap has :" + STATE.NEW + " : "
                            +  stateMap.containsKey(STATE.NEW));
     
        //Java EnumMap Example 8: checking if EnumMap contains a item value
        System.out.println("Does stateMap has :" + STATE.NEW + " : " + stateMap.containsValue(null));

    }
 
}

Output:
Size of EnumMap inwards java: 4
EnumMap: {NEW=Program has only created, RUNNING=Program is running, WAITING=Program is waiting, FINISHED=Program has finished}

EnumMap cardinal : NEW value: Program has only created
key : NEW value : Program has only created
key : RUNNING value : Program is running
key : WAITING value : Program is waiting
key : FINISHED value : Program has finished
Does stateMap has :NEW : true
Does stateMap has :NEW : false


In summary if you lot are using enum keys or tin role enum keys prefer EnumMap over HashMap or whatever other Map implementation because EnumMap is specialized Map implementation for enum in addition to provides amend performance than full general map. In this Java tutorial nosotros choose seen What is EnumMap inwards Java, of import points almost EnumMap inwards Java in addition to How to role EnumMap amongst around how to type of examples.

Further Learning
Java In-Depth: Become a Complete Java Engineer
4 ways to traverse Map inwards Java