EnumMap entrySet() Method in Java
Last Updated :
24 Jul, 2020
The Java.util.EnumMap.entrySet() method in Java used to create a set out of the elements contained in the EnumMap. It basically returns a set view of the enum map.
Syntax:
enum_map.entrySet()
Parameter: The method does not take any parameters.
Return Value: The method returns the set view of mappings contained in this enum_map.
Below programs illustrate the entrySet() method:
Program 1:
import java.util.*;
public enum rank_countries {
India,
United_States,
China,
Japan,
Canada,
Russia
}
;
class Enum_map {
public static void main(String[] args)
{
EnumMap<rank_countries, Integer> mp = new
EnumMap<rank_countries, Integer>(rank_countries. class );
mp.put(rank_countries.India, 72 );
mp.put(rank_countries.United_States, 1083 );
mp.put(rank_countries.China, 4632 );
mp.put(rank_countries.Japan, 6797 );
System.out.println( "Map view: " + mp);
Set<Map.Entry<rank_countries, Integer> > set_view =
mp.entrySet();
System.out.println( "Set view of the map: " + set_view);
}
}
|
Output:
Map view: {India=72, United_States=1083, China=4632, Japan=6797}
Set view of the map: [India=72, United_States=1083, China=4632, Japan=6797]
Program 2:
import java.util.*;
public enum gfg {
Global_2018,
India_2018
};
class Enum_map {
public static void main(String[] args)
{
EnumMap<gfg, Integer> mp = new
EnumMap<gfg, Integer>(gfg. class );
mp.put(gfg.Global_2018, 800 );
mp.put(gfg.India_2018, 72 );
System.out.println( "Mapping view: " + mp);
Set<Map.Entry<gfg, Integer> > set_view =
mp.entrySet();
System.out.println( "Set view of the map: "
+ set_view);
}
}
|
Output:
Mapping view: {Global_2018=800, India_2018=72}
Set view of the map: [Global_2018=800, India_2018=72]
Share your thoughts in the comments
Please Login to comment...