Java.util.Hashtable se utiliza para crear un conjunto de elementos clave en la tabla hash.
Sintaxis:
public Set<K> keySet()
K : tipo de Claves en la tabla hash
Parámetros: El método no toma ningún parámetro.
Valor devuelto: el método devuelve un conjunto que tiene las claves de la tabla hash.
Los siguientes programas se utilizan para ilustrar el funcionamiento del método java.util.Hashtable.keySet():
Ejemplo 1:
Java
// Java code to illustrate the keySet() method // to get Set view of Keys from a Hashtable. import java.util.Enumeration; import java.util.Iterator; import java.util.Hashtable; import java.util.Set; public class Example1 { public static void main(String[] args) { // Creating an empty Hashtable Hashtable<String, String> hash_t = new Hashtable<String, String>(); // Add mappings into the table hash_t.put("1", "Geeks"); hash_t.put("2", "For"); hash_t.put("3", "Geeks"); // Getting a Set of keys using // keySet() method of Hashtable class Set hash_set = hash_t.keySet(); System.out.println( "Set created from Hashtable Keys contains :"); // Iterating through the Set of keys Iterator itr = hash_set.iterator(); while (itr.hasNext()) System.out.println(itr.next()); } }
Producción
Set created from Hashtable Keys contains : 3 2 1
Ejemplo 2:
Java
// Java code to illustrate the keySet() method // to get Set view of Keys from a Hashtable. import java.util.Enumeration; import java.util.Iterator; import java.util.Hashtable; import java.util.Set; public class Example2 { public static void main(String[] args) { // Creating an empty Hashtable Hashtable<String, String> hash_t = new Hashtable<String, String>(); // Inserting elements into the table hash_t.put("Geeks", "1"); hash_t.put("For", "2"); hash_t.put("geeks", "3"); // Getting a Set of keys using // keySet() method of Hashtable class Set hash_set = hash_t.keySet(); System.out.println( "Set created from Hashtable Keys contains :"); // Iterating through the Set of keys Iterator itr = hash_set.iterator(); while (itr.hasNext()) System.out.println(itr.next()); } }
Producción
Set created from Hashtable Keys contains : For Geeks geeks
Nota:
Publicación traducida automáticamente
Artículo escrito por sayaliparulekar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA