Java WeakHashMap put()方法

Java WeakHashMap put()方法

WeakHashMap的java.util.WeakHashMap.put()方法是用来将一个映射插入到一个地图中。这意味着我们可以将一个特定的键和它所映射的值插入到一个特定的地图中。如果传递的是一个现有的键,那么之前的值就会被新的值所取代。如果传递了一个新的配对,那么这个配对就会被作为一个整体插入。

语法

Weak_Hash_Map.put(key, value)

参数: 该方法需要两个参数,都是WeakHashMap的对象类型。

  • key。 这指的是需要插入到Map中进行映射的关键元素。
  • value(值)。 这指的是上述键会映射成的值。

返回值: 如果传递了一个现有的键,那么将返回之前的值。如果传递的是一个新的配对,则返回NULL。

下面的程序用来说明java.util.WeakHashMap.put()方法的工作:

程序1: 当传递一个现有的键时。

// Java code to illustrate the put() method
import java.util.*;
  
public class Weak_Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty WeakHashMap
        Map<Integer, String> weak_hash = new 
                  WeakHashMap<Integer, String>();
  
        // Mapping string values to int keys
        weak_hash.put(10, "Geeks");
        weak_hash.put(15, "4");
        weak_hash.put(20, "Geeks");
        weak_hash.put(25, "Welcomes");
        weak_hash.put(30, "You");
  
        // Displaying the WeakHashMap
        System.out.println("Initial Mappings are: " + 
                                           weak_hash);
  
        // Inserting existing key along with new value
        String returned_value = 
                     (String)weak_hash.put(20, "All");
  
        // Verifying the returned value
        System.out.println("Returned value is: " + 
                                   returned_value);
  
        // Displayin the new map
        System.out.println("New map is: " + weak_hash);
    }
}

输出:

Initial Mappings are: {30=You, 15=4, 10=Geeks, 25=Welcomes, 20=Geeks}
Returned value is: Geeks
New map is: {30=You, 15=4, 10=Geeks, 25=Welcomes, 20=All}

程序2: 当传递一个新的密钥时。

// Java code to illustrate the put() method
import java.util.*;
  
public class Weak_Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty WeakHashMap
        Map<Integer, String> weak_hash = new 
                 WeakHashMap<Integer, String>();
  
        // Mapping string values to int keys
        weak_hash.put(10, "Geeks");
        weak_hash.put(15, "4");
        weak_hash.put(20, "Geeks");
        weak_hash.put(25, "Welcomes");
        weak_hash.put(30, "You");
  
        // Displaying the WeakHashMap
        System.out.println("Initial Mappings are: " + 
                                          weak_hash);
  
        // Inserting existing key along with new value
        String returned_value = 
                      (String)weak_hash.put(50, "All");
  
        // Verifying the returned value
        System.out.println("Returned value is: " + 
                                      returned_value);
  
        // Displayin the new map
        System.out.println("New map is: " + weak_hash);
    }
}

输出:

Initial Mappings are: {30=You, 15=4, 10=Geeks, 25=Welcomes, 20=Geeks}
Returned value is: null
New map is: {30=You, 15=4, 10=Geeks, 25=Welcomes, 20=Geeks, 50=All}

注意: 同样的操作可以通过不同数据类型的变化和组合对任何类型的Mapping进行操作。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程