Método remove() de hashtable em Java
O java.util.Hashtable.remove() é um método embutido da classe Hashtable e é usado para remover o mapeamento de qualquer chave específica da tabela. Basicamente, ele remove os valores de qualquer chave específica da Tabela.
Sintaxe:
Hash_Table.remove(Object key)
Parâmetros: o método usa uma chave de parâmetro cujo mapeamento deve ser removido da Tabela.
Valor de retorno: O método retorna o valor que foi mapeado anteriormente para a chave especificada se a chave existir, caso contrário, o método retornará NULL.
Os programas abaixo ilustram o funcionamento do método java.util.Hashtable.remove():
Programa 1: Ao passar uma chave existente.
// Java code to illustrate the remove() 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 elements into the 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);
// Removing the existing key mapping
String returned_value = (String)hash_table.remove(20);
// Verifying the returned value
System.out.println("Returned value is: " + returned_value);
// Displaying the new table
System.out.println("New table is: " + hash_table);
}
}
A tabela inicial é: {10 = Geeks, 20 = Geeks, 30 = You, 15 = 4, 25 = Welcome} O valor retornado é: Geeks A nova mesa é: {10 = Geeks, 30 = You, 15 = 4, 25 = Welcome}
Programa 2: Ao passar uma nova chave.
// Java code to illustrate the remove() 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 mappings into the 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);
// Removing the new key mapping
String returned_value = (String)hash_table.remove(50);
// Verifying the returned value
System.out.println("Returned value is: " + returned_value);
// Displaying the new table
System.out.println("New table is: " + hash_table);
}
}
A tabela inicial é: {10 = Geeks, 20 = Geeks, 30 = You, 15 = 4, 25 = Welcome} O valor retornado é: nulo A nova mesa é: {10 = Geeks, 20 = Geeks, 30 = You, 15 = 4, 25 = Welcome}
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