Java TreeMap containsValue()方法及示例

Java TreeMap containsValue()方法及示例

在Java中,TreeMap类的containsValue()方法 用于检查一个特定的值是否被TreeMap中的任何键所映射。它以值为参数,如果该值被地图中的任何一个键映射,则返回True。

--> java.util Package 
    --> TreeMap class
        --> containsValue() Method  

语法

Tree_Map.containsValue( _Object Value_ )

参数: 对象类型的值 ,指的是其映射应该被地图内的任何键检查的值。( 只有一个参数)

返回类型: 一个布尔值,如果该值的映射被检测到则为真,否则为假。

注意 :在java中,当返回布尔值时,只返回true false,而不是0和1,因为这是不允许的,所以在编译时将产生错误。

例1: 将字符串值映射到整数键。

// Java Program to Illustrate containsValue() Method
// of TreeMap class
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // main driver method
    public static void main(String[] args)
    {
        // Creating an empty TreeMap by
        // declaring object of integer and string pairs
        TreeMap<Integer, String> tree_map
            = new TreeMap<Integer, String>();
 
        // Mapping string values to int keys
        // using put() method
        tree_map.put(10, "Geeks");
        tree_map.put(15, "4");
        tree_map.put(20, "Geeks");
        tree_map.put(25, "Welcomes");
        tree_map.put(30, "You");
 
        // Printing the elements of TreeMap
        System.out.println("Initial Mappings are: "
                           + tree_map);
 
        // Checking for custom value in map
        // using containsValue() method
 
        // Say for the Value 'Geeks'
        System.out.println(
            "Is the value 'Geeks' present? "
            + tree_map.containsValue("Geeks"));
 
        // Checking for the Value 'World'
        System.out.println(
            "Is the value 'World' present? "
            + tree_map.containsValue("World"));
    }
}

输出

Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
Is the value 'Geeks' present? true
Is the value 'World' present? false

例2: 将整数值映射到字符串键。

// Java Program  to illustrate containsValue() Method
// of TreeMap class
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an empty TreeMap by
        // declaring object of string, integer pairs
        TreeMap<String, Integer> tree_map
            = new TreeMap<String, Integer>();
 
        // Mapping int values to string keys
        // using put() method
        tree_map.put("Geeks", 10);
        tree_map.put("4", 15);
        tree_map.put("Geeks", 20);
        tree_map.put("Welcomes", 25);
        hastree_map_map.put("You", 30);
 
        // Printing the elements of TreeMap
        System.out.println("Initial Mappings are: "
                           + tree_map);
 
        // Checking for the Value '10'
        // using containsValue() method
        System.out.println("Is the value '10' present? "
                           + tree_map.containsValue(10));
 
        // Checking for the Value '30'
        System.out.println("Is the value '30' present? "
                           + tree_map.containsValue(30));
 
        // Checking for the Value '40'
        System.out.println("Is the value '40' present? "
                           + tree_map.containsValue(40));
    }
}

输出

Java中的TreeMap containsValue方法及示例

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程