Método Hashtable toString() em Java
O java.util.Hashtable.toString() é um método embutido de Hashtable que é usado para obter uma representação de string dos objetos de Hashtable na forma de um conjunto de entradas separadas por “,“. Então, basicamente, o método toString() é usado para converter todos os elementos de Hashtable em String.
Sintaxe:
Hash_Table.toString()
Parâmetro: o método não aceita nenhum parâmetro.
Valor de retorno: o método retorna o conjunto que consiste na representação de string dos elementos da tabela hash.
Os programas abaixo ilustram o funcionamento do método java.util.Hashtable.toString():
Programa 1:
// Java code to illustrate the toString() 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);
// Displaying the string representation
System.out.println("The set is: " + hash_table.toString());
}
}
A tabela inicial é: {10 = Geeks, 20 = Geeks, 30 = You, 15 = 4, 25 = Welcome} O conjunto é: {10 = Geeks, 20 = Geeks, 30 = You, 15 = 4, 25 = Welcome}
Programa 2:
// Java code to illustrate the toString() 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 elements into 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);
// Displaying the string representation
System.out.println("The set is: " + hash_table.toString());
}
}
A tabela inicial é: {You = 30, Welcome = 25, 4 = 15, Geeks = 20} O conjunto é: {You = 30, Welcome = 25, 4 = 15, Geeks = 20}
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