O método java.util.Hashtable.put() de Hashtable é usado para inserir um mapeamento em uma tabela. Isso significa que podemos inserir uma chave específica e o valor para o qual ela está mapeando em uma tabela específica. Se uma chave existente for passada, o valor anterior será substituído pelo novo valor. Se um novo par for passado, o par será inserido como um todo.

Sintaxe:

Hash_Table.put(key, value)

Parâmetros: O método leva dois parâmetros, ambos são do tipo Object da Hashtable.

  • chave: Refere-se ao elemento-chave que precisa ser inserido na Tabela para mapeamento.
  • valor: refere-se ao valor para o qual a chave acima seria mapeada.

Valor de retorno: se uma chave existente for passada, o valor anterior será retornado. Se um novo par for passado, NULL será retornado.

Os programas abaixo são usados ​​para ilustrar o funcionamento do Método java.util.Hashtable.put():
Programa 1: Ao passar uma chave existente.

// Java code to illustrate the put() 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 values 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);
  
        // Inserting existing key along with new value
        String returned_value = (String)hash_table.put(20, "All");
  
        // Verifying the returned value
        System.out.println("Returned value is: " + returned_value);
  
        // Displaying the new table
        System.out.println("New table is: " + hash_table);
    }
}
Saída:
A tabela inicial é: {10 = Geeks, 20 = Geeks, 30 = You, 15 = 4, 25 = Welcome}
O valor retornado é: Geeks
A nova mesa é: {10 = Geeks, 20 = All, 30 = You, 15 = 4, 25 = Welcome}

Programa 2: Ao passar uma nova chave.

// Java code to illustrate the put() 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 values 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);
  
        // Inserting existing key along with new value
        String returned_value = (String)hash_table.put(50, "All");
  
        // Verifying the returned value
        System.out.println("Returned value is: " + returned_value);
  
        // Displaying the new table
        System.out.println("New table is: " + hash_table);
    }
}
Saída:
A tabela inicial é: {10 = Geeks, 20 = Geeks, 30 = You, 15 = 4, 25 = Welcome}
O valor retornado é: nulo
A nova tabela é: {10 = Geeks, 20 = Geeks, 30 = You, 50 = All, 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.