What Is Enummap Inwards Coffee – Example Tutorial

What is EnumMap inwards Java
EnumMap inwards Java is added on JDK  5 liberate along alongside 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 upward for a type (like shape or interface)  in Java in addition to it tin also override equals() in addition to hashCode() , It can hold upward used within HashMap or whatever other collection but using  EnumMap brings implementation specific benefits which are done for enum keys, In brusk EnumMap is optimized Map implementation solely 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 hold upward a decent alternative for enumeration data. We select 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 Federal Reserve notation Difference betwixt EnumMap in addition to HashMap is likewise getting pop equally ane of advanced Java collection interviews questions in addition to most of Java IDE similar Eclipse in addition to Netbeans volition likewise propose to usage EnumMap if you lot usage Enum keys along alongside HashMap equally purpose of in that place code improvement suggestion.


Important points of EnumMap in Java:
 liberate along alongside other of import features similar What is EnumMap inwards Java – Example TutorialHere is few of import points to think near EnumMap inwards Java which is likewise useful spell using EnumMap inwards code to avoid whatever compile fourth dimension or logical errors :


1. All keys used inwards EnumMap must hold upward  from same Enum type which is specified spell creating EnumMap in Java. For representative if you lot tin non usage dissimilar enum instances from ii dissimilar enum.

2. EnumMap is ordered collection and they are maintained inwards the natural lodge of their keys( natural lodge of keys means  the lodge 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 exhibit consequence of whatever modification on EnumMap during Iteration process.

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

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

6. EnumMap is probable laissez passer on ameliorate performance than HashMap inwards Java. So prefer EnumMap if you lot are going to usage enum keys.

How to usage EnumMap inwards Java – EnumMap Example

In this department nosotros volition come across How to usage EnumMap inwards Java alongside 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 detail telephone commutation in addition to value or non etc. All of these operations are similar to other Map implementation similar HashMap and doesn’t postulate especial cognition but It’s skillful to think 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 usage EnumMap inwards Java
 * If Key Object is Enum than it’s best to EnumMap to instruct ameliorate performance.
 * Most of IDE similar Netbeans in addition to Eclipse propose you lot to usage EnumMap instead of HashMap
 * or whatever other Map implementation when telephone commutation 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 alongside telephone commutation 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 dissimilar lodge than their natural order
        stateMap.put(STATE.RUNNING, "Program is running");
        stateMap.put(STATE.WAITING, "Program is waiting");
        stateMap.put(STATE.NEW, "Program has merely 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 lodge
        //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 telephone commutation : " + 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 detail key
        System.out.println("Does stateMap has :" + STATE.NEW + " : "
                            +  stateMap.containsKey(STATE.NEW));
     
        //Java EnumMap Example 8: checking if EnumMap contains a detail value
        System.out.println("Does stateMap has :" + STATE.NEW + " : " + stateMap.containsValue(null));

    }
 
}

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

EnumMap telephone commutation : NEW value: Program has merely created
key : NEW value : Program has merely 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 usage enum keys prefer EnumMap over HashMap or whatever other Map implementation because EnumMap is specialized Map implementation for enum in addition to provides ameliorate performance than full general map. In this Java tutorial nosotros select seen What is EnumMap inwards Java, of import points near EnumMap inwards Java in addition to How to usage EnumMap alongside to a greater extent than or less how to type of examples.

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

Belum ada Komentar untuk "What Is Enummap Inwards Coffee – Example Tutorial"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel