What Is Enummap Inwards Coffee – Example Tutorial

What is EnumMap inwards Java
EnumMap inwards Java is added on JDK  5 liberate along amongst other of import features like  Autoboxing, varargs as well as Generics. EnumMap is specialized Map implementation designed as well as optimized for using Java Enum every bit key. Since enum tin flame stand upwards for a type (like shape or interface)  in Java as well as it tin flame also override equals() as well as 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 solely for enum keys . As per Javadoc Enum is implemented using Arrays as well as mutual operations final result inwards constant time. So if yous are thinking of an high-performance Map, EnumMap could survive a decent choice for enumeration data. We accept already seen many examples of Java enum inwards our article 10 Examples of enum inwards Java  and using Enum every bit thread-safe Singleton. In this Java tutorial, nosotros volition encounter elementary examples of using EnumMap in Java. 

On related banker's complaint Difference betwixt EnumMap as well as HashMap is besides getting pop every bit i of advanced Java collection interviews questions as well as most of Java IDE similar Eclipse as well as Netbeans volition besides propose to purpose EnumMap if yous purpose Enum keys along amongst HashMap every bit business office of in that place code improvement suggestion.


Important points of EnumMap in Java:
 liberate along amongst other of import features similar What is EnumMap inwards Java – Example TutorialHere is few of import points to recall close EnumMap inwards Java which is besides useful acre 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 acre creating EnumMap in Java. For representative if yous tin flame non purpose unlike enum instances from 2 unlike 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 ). yous tin flame verify this acre Iterating over an EnumMap in Java.

3. Iterators of EnumMap are fail-fast Iterator , much similar of ConcurrentHashMap as well as doesn't throw ConcurrentModificationException as well as may non demo final result of whatever alteration on EnumMap during Iteration process.

4. You tin flame non insert null keys within EnumMap inwards Java.  EnumMap doesn't allow null fundamental as well as throw NullPointerException, at same fourth dimension nix values are permitted.

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

6. EnumMap is probable laissez passer on meliorate performance than HashMap inwards Java. So prefer EnumMap if yous are going to purpose enum keys.

How to purpose EnumMap inwards Java – EnumMap Example

In this department nosotros volition encounter How to purpose EnumMap inwards Java amongst elementary 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 fundamental as well as value or non etc. All of these operations are similar to other Map implementation similar HashMap and doesn’t demand particular noesis but It’s expert to recall all points specific to EnumMap every bit discussed inwards previous department to avoid whatever error.


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

/**
 * Java programme to demonstrate How to purpose EnumMap inwards Java
 * If Key Object is Enum than it’s best to EnumMap to larn meliorate performance.
 * Most of IDE similar Netbeans as well as Eclipse propose yous to purpose EnumMap instead of HashMap
 * or whatever other Map implementation when fundamental 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 fundamental every bit 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 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 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 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 fundamental : " + 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 only created, RUNNING=Program is running, WAITING=Program is waiting, FINISHED=Program has finished}

EnumMap fundamental : 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 yous are using enum keys or tin flame purpose enum keys prefer EnumMap over HashMap or whatever other Map implementation because EnumMap is specialized Map implementation for enum as well as provides meliorate performance than full general map. In this Java tutorial nosotros accept seen What is EnumMap inwards Java, of import points close EnumMap inwards Java as well as How to purpose EnumMap amongst 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

Komentar

Postingan populer dari blog ini

Common Multi-Threading Mistakes Inwards Coffee - Calling Run() Instead Of Start()

3 Examples Of Parsing Html File Inwards Coffee Using Jsoup

Why You Lot Should Command Visibility Of Shape Too Interface Inward Java