當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java TreeMap containsValue()用法及代碼示例


在 Java ,containsValue()方法TreeMap 類用於檢查特定值是否被 TreeMap 中的任何鍵映射。它將該值作為參數,如果該值由映射中的任何鍵映射,則返回 True。

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

用法:

Tree_Map.containsValue(Object Value)

參數:的v阿盧埃對象類型,指的是映射中的任何鍵都應該檢查其映射的值。 (隻有一個參數)

返回類型:布爾值,如果檢測到值的映射則為 true,否則為 false。

Note: In java, while returning boolean values are returned as true false only not as 0 and 1 as it is not allowed so do will throw error at compilation.

示例 1:將字符串值映射到整數鍵。

Java


// 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


// 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));
    }
}

輸出:

Note: Similarly the same operation can be performed with any type of Mappings with variation and combination of different data types.



相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 TreeMap containsValue() Method in Java With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。