Método get() de hashtable em Java
O método java.util.Hashtable.get() da classe Hashtable é usado para recuperar ou buscar o valor mapeado por uma chave específica mencionada no parâmetro. Ele retorna NULL quando a tabela não contém tal mapeamento para a chave.
Sintaxe:
Hash_Table.get(Object key_element)
Parâmetro: o método recebe um parâmetro key_element do tipo de objeto e se refere à chave cujo valor associado deve ser buscado.
Valor de retorno: o método retorna o valor associado ao key_element no parâmetro.
Os programas abaixo ilustram o funcionamento do método java.util.Hashtable.get():
Programa 1:
// Java code to illustrate the get() method
import java.util.*;
public class Hash_Table_Demo {
public static void main(String[] args)
{
// Creating an empty Hashtable
Hashtable<Integer, String> hash_table =
new Hashtable<Integer, String>();
// Inserting the values into table
hash_table.put(10, "Geeks");
hash_table.put(15, "4");
hash_table.put(20, "Geeks");
hash_table.put(25, "Welcomes");
hash_table.put(30, "You");
// Displaying the Hashtable
System.out.println("Initial Table is: " + hash_table);
// Getting the value of 25
System.out.println("The Value is: " + hash_table.get(25));
// Getting the value of 10
System.out.println("The Value is: " + hash_table.get(10));
}
}
A tabela inicial é: {10 = Geeks, 20 = Geeks, 30 = You, 15 = 4, 25 = Welcome} O valor é: Boas-vindas O valor é: Geeks
Programa 2:
// Java code to illustrate the get() method
import java.util.*;
public class Hash_Table_Demo {
public static void main(String[] args)
{
// Creating an empty Hashtable
Hashtable<String, Integer> hash_table =
new Hashtable<String, Integer>();
// Inserting the values into table
hash_table.put("Geeks", 10);
hash_table.put("4", 15);
hash_table.put("Geeks", 20);
hash_table.put("Welcomes", 25);
hash_table.put("You", 30);
// Displaying the Hashtable
System.out.println("Initial table is: " + hash_table);
// Getting the value of "Geeks"
System.out.println("The Value is: " + hash_table.get("Geeks"));
// Getting the value of "You"
System.out.println("The Value is: " + hash_table.get("You"));
}
}
A tabela inicial é: {You = 30, Welcome = 25, 4 = 15, Geeks = 20} O valor é: 20 O valor é: 30
Nota: A mesma operação pode ser realizada com qualquer tipo de variação e combinação de diferentes tipos de dados.
As postagens do blog Acervo Lima te ajudaram? Nos ajude a manter o blog no ar!
Faça uma doação para manter o blog funcionando.
70% das doações são no valor de R$ 5,00...
Diógenes Lima da Silva