What Is Enummap Inwards Coffee – Illustration Tutorial

What is EnumMap inward Java
EnumMap inward Java is added on JDK  5 unloose along amongst other of import features like  Autoboxing, varargs together with Generics. EnumMap is specialized Map implementation designed together with optimized for using Java Enum equally key. Since enum tin reach the axe correspond a type (like class or interface)  in Java together with it tin reach the axe also override equals() together with hashCode() , It can hold out 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 alone for enum keys . As per Javadoc Enum is implemented using Arrays together with mutual operations outcome inward constant time. So if you lot are thinking of an high-performance Map, EnumMap could hold out a decent selection for enumeration data. We choose already seen many examples of Java enum inward our article 10 Examples of enum inward Java  and using Enum equally thread-safe Singleton. In this Java tutorial, nosotros volition run across uncomplicated examples of using EnumMap in Java. 

On related banking concern notation Difference betwixt EnumMap together with HashMap is besides getting pop equally 1 of advanced Java collection interviews questions together with most of Java IDE similar Eclipse together with Netbeans volition besides propose to role EnumMap if you lot role Enum keys along amongst HashMap equally business office of at that spot code improvement suggestion.


Important points of EnumMap in Java:
 unloose along amongst other of import features similar What is EnumMap inward Java – Example TutorialHere is few of import points to think most EnumMap inward Java which is besides useful piece using EnumMap inward code to avoid whatever compile fourth dimension or logical errors :


1. All keys used inward EnumMap must hold out  from same Enum type which is specified piece creating EnumMap in Java. For representative if you lot tin reach the axe non role unlike enum instances from 2 unlike enum.

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

3. Iterators of EnumMap are fail-fast Iterator , much similar of ConcurrentHashMap together with doesn't throw ConcurrentModificationException together with may non present number of whatever alteration on EnumMap during Iteration process.

4. You tin reach the axe non insert null keys within EnumMap inward Java.  EnumMap doesn't allow null primal together with throw NullPointerException, at same fourth dimension nothing values are permitted.

5. EnumMap is non synchronized together with it has to hold out synchronized manually earlier using it inward a concurrent or multi-threaded environment. similar synchronized Map inward Java  you tin reach the axe besides brand EnumMap synchronized past times using Collections.synchronizedMap() method together with equally per javadoc this should hold out done piece creating EnumMap in coffee to avoid accidental non synchronized access.

6. EnumMap is probable reach meliorate performance than HashMap inward Java. So prefer EnumMap if you lot are going to role enum keys.

How to role EnumMap inward Java – EnumMap Example

In this department nosotros volition run across How to role EnumMap inward Java amongst uncomplicated examples similar creating EnumMap, putting objects into EnumMap, getting objects from EnumMap,  finding size of EnumMap, Iterating over EnumMap, printing EnumMap inward console , checking if EnumMap contains a detail primal together with value or non etc. All of these operations are similar to other Map implementation similar HashMap and doesn’t take away exceptional noesis but It’s expert to think all points specific to EnumMap equally discussed inward previous department to avoid whatever error.


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

/**
 * Java programme to demonstrate How to role EnumMap inward Java
 * If Key Object is Enum than it’s best to EnumMap to instruct meliorate performance.
 * Most of IDE similar Netbeans together with Eclipse propose you lot to role EnumMap instead of HashMap
 * or whatever other Map implementation when primal 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 inward coffee amongst primal equally enum type STATE
        EnumMap<STATE, String> stateMap = new EnumMap<STATE, String>(STATE.class);
     
        // Java EnumMap Example 2:
        //putting values within EnumMap inward Java
        //we are inserting Enum keys on unlike gild than their natural order
        stateMap.put(STATE.RUNNING, "Program is running");
        stateMap.put(STATE.WAITING, "Program is waiting");
        stateMap.put(STATE.NEW, "Program has simply created");
        stateMap.put(STATE.FINISHED, "Program has finished");
     
        // Java EnumMap Example 3:
        //printing size of EnumMap inward java
        System.out.println("Size of EnumMap inward java: " + stateMap.size());
     
        // Java EnumMap Example 5:
        //printing Java EnumMap , should impress EnumMap inward natural gild
        //of enum keys (order on which they are declared)
        System.out.println("EnumMap: " + stateMap);
     
        // Java EnumMap Example 5:
        //retrieving value from EnumMap inward java
        System.out.println("EnumMap primal : " + 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 inward java: 4
EnumMap: {NEW=Program has simply created, RUNNING=Program is running, WAITING=Program is waiting, FINISHED=Program has finished}

EnumMap primal : NEW value: Program has simply created
key : NEW value : Program has simply 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 reach the axe role enum keys prefer EnumMap over HashMap or whatever other Map implementation because EnumMap is specialized Map implementation for enum together with provides meliorate performance than full general map. In this Java tutorial nosotros choose seen What is EnumMap inward Java, of import points most EnumMap inward Java together with How to role EnumMap amongst about how to type of examples.

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

0 Response to "What Is Enummap Inwards Coffee – Illustration Tutorial"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel