This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Previously we have discussed about
what is properties class in java ?
Here, we learn What is Enum maps In Java.But before starting we must know what is Map Interface In Java.? And What is Enum in Java ?
Class EnumMap<K extends Enum<K>,V>
All Implemented Interfaces :-
Declaration of EnumMap:
public class EnumMap<K extends Enum<K>,V>
extends AbstractMap<K,V>
implements Serializable, Cloneable
Enum maps belongs to java.util.*; package .Enum maps are maintained in the natural order of their keys (the order in which the enum constants are declared).
Null keys are not permitted but Null values are permitted.If we try to put any null key it will throw NullPointerException.
EnumMap is fail-fast iterator and does not throw ConcurrentModificationException.
EnumMap implementation is not synchronized.If multiple threads access an enum map concurrently, and at least one of the threads modifies the Map means that thread can adds or deletes one or more mappings.To prevent this we can synchronized the EnumMap using
Map<EnumKey, V> m = Collections.synchronizedMap(new EnumMap<EnumKey, V>(...));
This class is a member of the Java Collections Framework.
Constructor and Description :-
EnumMap(Class<K> keyType)
Creates an empty enum map with the specified key type.
EnumMap(EnumMap<K,? extends V> m)
Creates an enum map with the same key type as the specified enum map, initially containing the same mappings (if any).
EnumMap(Map<K,? extends V> m)
Creates an enum map initialized from the specified map.
Example of EnumMap Class :-
In this example i will show you how to use EnumMap ,
import java.util.EnumMap;
public class EnumMapExample {
public enum VIOWEL {
A,E,I,O,U;
}
public static void main(String[] args) {
EnumMap<VIOWEL, String> m = new EnumMap<VIOWEL,String>(VIOWEL.class);
m.put(VIOWEL.A, "First Alphabet Viowel");
m.put(VIOWEL.E, "Second Alphabet Viowel");
m.put(VIOWEL.I, "Third Alphabet Viowel");
m.put(VIOWEL.O, "Fourth Alphabet Viowel");
m.put(VIOWEL.U, "Fifth Alphabet Viowel");
System.out.println("ENUM MAP SIZE : "+m.size());
System.out.println("ENUM MAP : "+m);
System.out.println("GETTING VALUE OF ENUM E : "+m.get(VIOWEL.E));
System.out.println("\nITERATION OVER ENUM MAP USING LAMBDA EXPRESSION");
m.forEach((k,v) -> System.out.println(k+"\t:\t"+v));
}
}
Program Output :-
ENUM MAP SIZE : 5
COMPLETE ENUM MAP : {A=First Alphabet Viowel, E=Second Alphabet Viowel, I=Third Alphabet Viowel, O=Fourth Alphabet Viowel, U=Fifth Alphabet Viowel}
GETTING VALUE OF ENUM E : Second Alphabet Viowel
ITERATION OVER ENUM MAP USING LAMBDA EXPRESSION
A : First Alphabet Viowel
E : Second Alphabet Viowel
I : Third Alphabet Viowel
O : Fourth Alphabet Viowel
U : Fifth Alphabet Viowel
Java I/O Tutorial
No comments:
Post a Comment