El método Java.util.EnumMap.containsKey( key ) se utiliza para verificar si una clave específica mencionada en el parámetro está presente en este mapa o no.
Sintaxis:
boolean containsKey(Object key)
Parámetro: El método acepta una clave de parámetro que se refiere a la clave a verificar.
Valor devuelto: el método devuelve verdadero si la clave está presente en EnumMap; de lo contrario, devuelve falso.
Los siguientes programas ilustran el método containsKey():
Programa 1:
// Java program to demonstrate containsKey() method import java.util.*; // An enum of gfg visitors is created public enum gfg_visitors { India, United_States, China, Japan, Canada }; class Enum_map { public static void main(String[] args) { EnumMap<gfg_visitors, String> mp = new EnumMap<gfg_visitors, String>(gfg_visitors.class); // values are associated in mp mp.put(gfg_visitors.India, "61.4%"); mp.put(gfg_visitors.United_States, "18.4%"); mp.put(gfg_visitors.China, "2.5%"); mp.put(gfg_visitors.Japan, "1.1%"); mp.put(gfg_visitors.Canada, "1.1%"); // Check if map contains gfg visitor from United_States boolean ans = mp.containsKey(gfg_visitors.United_States); // Prints the result System.out.println("gfg_visitors from United States: " + ans); } }
Producción:
gfg_visitors from United States: true
Programa 2:
// Java program to demonstrate containsKey() method import java.util.*; // An enum of gfg visitors is created public enum gfg_visitors { India, United_States, China, Japan, Canada, Russia }; class Enum_map { public static void main(String[] args) { EnumMap<gfg_visitors, String> mp = new EnumMap<gfg_visitors, String>(gfg_visitors.class); // values are associated in mp mp.put(gfg_visitors.India, "61.4%"); mp.put(gfg_visitors.United_States, "18.4%"); mp.put(gfg_visitors.China, "2.5%"); mp.put(gfg_visitors.Japan, "1.1%"); mp.put(gfg_visitors.Canada, "1.1%"); // check if map contains gfg visitor from Russia boolean ans = mp.containsKey(gfg_visitors.Russia); // prints the result System.out.println("gfg_visitors from Russia: " + ans); } }
Producción:
gfg_visitors from Russia: false