Método Hashtable containsValue() em Java
O método java.util.Hashtable.containsValue() é usado para verificar se um determinado valor está sendo mapeado por uma única ou mais de uma chave na Hashtable. Ele pega o valor como parâmetro e retorna True se esse valor for mapeado por qualquer uma das chaves da tabela.
Sintaxe:
Hash_Table.containsValue(Object Value)
Parâmetros: O método aceita apenas um parâmetro Valor do tipo Objeto e se refere ao valor cujo mapeamento deve ser verificado por qualquer chave dentro da tabela.
Valor de retorno: o método retorna verdadeiro booleano se o valor for mapeado para qualquer chave na tabela de hash, caso contrário, ele retorna falso.
Os programas abaixo são usados para ilustrar o funcionamento do Método java.util.Hashtable.containsValue():
Programa 1:
// Java code to illustrate the containsValue() 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>();
// Putting values in 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);
// Checking for the Value 'Geeks'
System.out.println("Is the value 'Geeks' present? " +
hash_table.containsValue("Geeks"));
// Checking for the Value 'World'
System.out.println("Is the value 'World' present? " +
hash_table.containsValue("World"));
}
}
A tabela inicial é: {10 = Geeks, 20 = Geeks, 30 = You, 15 = 4, 25 = Welcome} O valor 'Geeks' está presente? verdade O valor 'Mundo' está presente? falso
Programa 2:
// Java code to illustrate the containsValue() 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>();
// Putting values in the 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);
// Checking for the Value '10'
System.out.println("Is the value '10' present? " +
hash_table.containsValue(10));
// Checking for the Value '30'
System.out.println("Is the value '30' present? " +
hash_table.containsValue(30));
// Checking for the Value '40'
System.out.println("Is the value '40' present? " +
hash_table.containsValue(40));
}
}
A tabela inicial é: {You = 30, Welcome = 25, 4 = 15, Geeks = 20} O valor '10' está presente? falso O valor '30' está presente? verdade O valor '40' está presente? falso
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