IdentityHashMap containsValue() Method in Java

IdentityHashMap containsValue() Method in Java

java.util.IdentityHashMap.containsValue()方法用于检查一个特定的值是否被一个或多个键映射到IdentityHashMap中。它将值作为参数,并返回True,如果该值被映射到映射中的任何键则为True。

语法:

Identity_HashMap.containsValue(Object Value)

参数: 该方法只有一个类型为Object的参数Value,它指向需要在地图内任何键检查其映射的值。

返回值: 如果检测到值的映射,则该方法返回bool值true,否则返回false。

下面的程序演示java.util.IdentityHashMap.containsValue()方法:

程序1: 将String值映射到Integer键。

// Java代码以说明containsValue()方法
import java.util.*;
  
public class Identity_Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // 创建一个空的IdentityHashMap
        IdentityHashMap<Integer, String> identity_hash = 
                   new IdentityHashMap<Integer, String>();
  
        // 将字符串值映射到整数键
        identity_hash.put(10, "Geeks");
        identity_hash.put(15, "4");
        identity_hash.put(20, "Geeks");
        identity_hash.put(25, "Welcomes");
        identity_hash.put(30, "You");
  
        // 显示IdentityHashMap
        System.out.println("初始映射为:" +
                                          identity_hash);
  
        // 检查值“Geeks”
        System.out.println("值'Geeks'是否存在?" + 
                        identity_hash.containsValue("Geeks"));
  
        // 检查值“World”
        System.out.println("值'World'是否存在?" + 
                        identity_hash.containsValue("World"));
    }
}
初始映射为:{10=Geeks, 30=You, 20=Geeks, 25=Welcomes, 15=4}
值'Geeks'是否存在?true
值'World'是否存在?false

程序2: 将整数值映射到String键。

// Java代码以说明containsValue()方法
import java.util.*;
  
public class Identity_Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // 创建一个空的IdentityHashMap
        IdentityHashMap<String, Integer> identity_hash = 
                      new IdentityHashMap<String, Integer>();
  
        // 将整数值映射到字符串键
        identity_hash.put("Geeks", 10);
        identity_hash.put("4", 15);
        identity_hash.put("Geeks", 20);
        identity_hash.put("Welcomes", 25);
        identity_hash.put("You", 30);
  
        // 显示IdentityHashMap
        System.out.println("初始映射为:" + 
                                             identity_hash);
  
        // 检查值“10”
        System.out.println("值'10'是否存在?" + 
                           identity_hash.containsValue(10));
  
        // 检查值“30”
        System.out.println("值'30'是否存在?" + 
                           identity_hash.containsValue(30));
  
        // 检查值“40”
        System.out.println("值'40'是否存在?" + 
                          identity_hash.containsValue(40));
    }
}
初始映射为:{Geeks=20, Welcomes=25, You=30, 4=15}
值'10'是否存在?false
值'30'是否存在?true
值'40'是否存在?false

注意: 可以使用不同的数据类型的变化和组合来执行同样的操作,不限于本示例中的Mappings类型。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程