Java TreeMap containsValue() 方法



描述

Java TreeMap containsValue(Object value) 方法用於返回 true,如果此對映將一個或多個鍵對映到指定值。

宣告

以下是 java.util.TreeMap.containsValue() 方法的宣告。

public boolean containsValue(Object value)

引數

value − 這是要在此對映中測試其存在的值。

返回值

如果存在此值的對映,則方法呼叫返回 true;否則返回 false。

異常

檢查整數、整數對 TreeMap 中是否存在值示例

以下示例演示了 Java TreeMap containsValue() 方法的用法,用於檢查值是否存在於對映中。我們建立了一個 Integer、Integer 對的 TreeMap 物件。然後向對映中新增一些條目,並使用 containsValue() 檢查值是否存在,然後列印結果。

package com.tutorialspoint;

import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {
      
      // creating tree map 
      TreeMap<Integer, Integer> treemap = new TreeMap<>();

      // populating tree map
      treemap.put(2, 2);
      treemap.put(1, 1);
      treemap.put(3, 3);
      treemap.put(6, 6);
      treemap.put(5, 5);

      System.out.println("Checking value");
      System.out.println("3 exists: "+ treemap.containsValue(3));
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Checking value
3 exists: true

檢查整數、字串對 TreeMap 中是否存在值示例

以下示例演示了 Java TreeMap containsValue() 方法的用法,用於檢查值是否存在於對映中。我們建立了一個 Integer、String 對的 TreeMap 物件。然後向對映中新增一些條目,並使用 containsValue() 檢查值是否存在,然後列印結果。

package com.tutorialspoint;

import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {
      
      // creating tree map 
      TreeMap<Integer, String> treemap = new TreeMap<>();

      // populating tree map
      treemap.put(2, "two");
      treemap.put(1, "one");
      treemap.put(3, "three");
      treemap.put(6, "six");
      treemap.put(5, "five");

      System.out.println("Checking value");
      System.out.println("'three' exists: "+ treemap.containsValue("three"));
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Checking value
'three' exists: true

檢查整數、物件對 TreeMap 中是否存在值示例

以下示例演示了 Java TreeMap containsValue() 方法的用法,用於檢查值是否存在於對映中。我們建立了一個 Integer、Student 對的 TreeMap 物件。然後向對映中新增一些條目,並使用 containsValue() 檢查值是否存在,然後列印結果。

package com.tutorialspoint;

import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {
      
      // creating tree map 
      TreeMap<Integer, Student> treemap = new TreeMap<>();

      // populating tree map
      treemap.put(2, new Student(2, "Robert"));
      treemap.put(1, new Student(1, "Julie"));  
      treemap.put(3, new Student(3, "Adam"));
      treemap.put(6, new Student(6, "Julia"));
      treemap.put(5, new Student(5, "Tom"));

      System.out.println("Checking value");
      System.out.println("Adam exists: "+ treemap.containsValue(new Student(3, "Adam")));
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   @Override
   public boolean equals(Object obj) {
      if(obj == null) return false;
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Checking value
Adam exists: true
java_util_treemap.htm
廣告
© . All rights reserved.