📜  Java的哈希表 remove() 方法

📅  最后修改于: 2022-05-13 01:57:12.993000             🧑  作者: Mango

Java的哈希表 remove() 方法

Java.util.Hashtable.remove() 是 Hashtable 类的内置方法,用于从表中删除任何特定键的映射。它基本上删除了表中任何特定键的值。
句法:

Hash_Table.remove(Object key)

参数:该方法采用一个参数键,其映射将从表中删除。
返回值:如果键存在,则该方法返回先前映射到指定键的值,否则该方法返回 NULL。
下面的程序说明了Java.util.Hashtable.remove() 方法的工作:
程序 1:传递现有密钥时。

Java
// 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 hash_table =
        new Hashtable();
 
        // 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);
    }
}


Java
// 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 hash_table =
        new Hashtable();
 
        // 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);
    }
}


输出:
Initial Table is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
Returned value is: Geeks
New table is: {10=Geeks, 30=You, 15=4, 25=Welcomes}

程序2:传递新密钥时。

Java

// 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 hash_table =
        new Hashtable();
 
        // 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);
    }
}
输出:
Initial table is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
Returned value is: null
New table is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}

注意:可以对任何类型的变体和不同数据类型的组合执行相同的操作。