EnumMap entrySet() Method in Java

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:

// Java program to demonsrate entrySet() method
import java.util.*;

// An enum of gfg rank is created
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);

        // Values are associated in mp
        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);

        // Map view
        System.out.println("Map view: " + mp);

        // Creating a new set of the mappings
        // contained in map
        Set<Map.Entry<rank_countries, Integer> > set_view = 
                                                mp.entrySet();

        // Set view of the mappings
        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:


// Java program to demonsrate entrySet() method
import java.util.*;

// An enum of gfg ranking worldwide and in india
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);

        // Values are associated in mp
        mp.put(gfg.Global_2018, 800);
        mp.put(gfg.India_2018, 72);

        // Mapping view
        System.out.println("Mapping view: " + mp);

        // Creating a new set of the mappings 
        Set<Map.Entry<gfg, Integer> > set_view = 
                                    mp.entrySet();

        // Set view of the mappings
        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]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Practice Tags :

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.






User Actions