El método Java.util.EnumMap.keySet() en Java se usa para devolver la vista establecida de las claves contenidas en el mapa.
Sintaxis:
Enum_Map.keySet()
Parámetros: El método no acepta ningún argumento.
Valor devuelto: el método devuelve la vista establecida de las claves contenidas en el mapa.
Los siguientes programas ilustran la función keySet():
Programa 1:
// Java program to demonstrate keySet() import java.util.*; // An enum of geeksforgeeks public enum gfg { India, United_States, China }; class Enum_demo { public static void main(String[] args) { EnumMap<gfg, String> mp = new EnumMap<gfg, String>(gfg.class); // Values are associated mp.put(gfg.India, "61.7%"); mp.put(gfg.United_States, "18.2%"); mp.put(gfg.China, "2.5%"); // Prints the map System.out.println("Mappings: " + mp); // Store the set view of the key Set<gfg> set_view = mp.keySet(); // Print the result System.out.println("Set view of the key: " + set_view); } }
Producción:
Mappings: {India=61.7%, United_States=18.2%, China=2.5%} Set view of the key: [India, United_States, China]
Programa 2:
// Java program to demonstrate keySet() import java.util.*; // An enum of geeksforgeeks public enum gfg { Global_today, India_today }; class Enum_demo { public static void main(String[] args) { EnumMap<gfg, Integer> mp = new EnumMap<gfg, Integer>(gfg.class); // Values are associated mp.put(gfg.Global_today, 799); mp.put(gfg.India_today, 69); // Prints the map System.out.println("Mappings: " + mp); // Store the set view of the key Set<gfg> set_view = mp.keySet(); // Print the result System.out.println("Set view of the key: " + set_view); } }
Producción:
Mappings: {Global_today=799, India_today=69} Set view of the key: [Global_today, India_today]