What Is Enummap Inwards Coffee – Example Tutorial

What is EnumMap inward Java
EnumMap inward Java is added on JDK  5 unloose 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 sack stand upwardly for a type (like course of report or interface)  in Java as well as it tin sack also override equals() as well as hashCode() , It can last 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 only for enum keys . As per Javadoc Enum is implemented using Arrays as well as mutual operations outcome inward constant time. So if you lot are thinking of an high-performance Map, EnumMap could last a decent pick for enumeration data. We select already seen many examples of Java enum inward our article 10 Examples of enum inward Java  and using Enum every bit thread-safe Singleton. In this Java tutorial, nosotros volition come across uncomplicated examples of using EnumMap in Java. 

On related depository fiscal establishment complaint Difference betwixt EnumMap as well as HashMap is every bit good 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 every bit good advise to role EnumMap if you lot role Enum keys along amongst HashMap every bit 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 inward Java – Example TutorialHere is few of import points to retrieve most EnumMap inward Java which is every bit good useful spell using EnumMap inward code to avoid whatever compile fourth dimension or logical errors :


1. All keys used inward EnumMap must last  from same Enum type which is specified spell creating EnumMap in Java. For instance if you lot tin sack non role dissimilar enum instances from 2 dissimilar 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 sack verify this spell 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 demonstrate outcome of whatever modification on EnumMap during Iteration process.

4. You tin sack non insert null keys within EnumMap inward Java.  EnumMap doesn't allow null substitution as well as throw NullPointerException, at same fourth dimension aught values are permitted.

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

6. EnumMap is probable plow over amend 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 come 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 substitution as well as value or non etc. All of these operations are similar to other Map implementation similar HashMap and doesn’t bespeak particular cognition but It’s expert to retrieve all points specific to EnumMap every bit discussed inward previous department to avoid whatever error.


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

/**
 * Java plan to demonstrate How to role EnumMap inward Java
 * If Key Object is Enum than it’s best to EnumMap to teach amend performance.
 * Most of IDE similar Netbeans as well as Eclipse advise you lot to role EnumMap instead of HashMap
 * or whatever other Map implementation when substitution 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 substitution every bit 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 dissimilar 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 merely 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 substitution : " + 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 merely created, RUNNING=Program is running, WAITING=Program is waiting, FINISHED=Program has finished}

EnumMap substitution : 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 sack role enum keys prefer EnumMap over HashMap or whatever other Map implementation because EnumMap is specialized Map implementation for enum as well as provides amend performance than full general map. In this Java tutorial nosotros select seen What is EnumMap inward Java, of import points most EnumMap inward Java as well as How to role 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 inward Java


Sumber https://javarevisited.blogspot.com/

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

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel