Java IdentityHashMap containsValue() 方法


使用 containsValue() 方法檢查是否存在某個值。首先,建立一個 IdentityHashMap −

Map<String, Integer> m = new
IdentityHashMap<String, Integer>();

新增一些元素 −

m.put("1", 100);
m.put("2", 200);
m.put("3", 300);
m.put("4", 150);
m.put("5", 110);
m.put("6", 50);
m.put("7", 90);
m.put("8", 250);
m.put("9", 350);
m.put("10", 450);

現在檢查是否存在某個值 −

m.containsValue(100))

以下是實現 containsValue() 方法的示例 −

示例

 實際演示

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      Map<String, Integer> m = new IdentityHashMap<String, Integer>();
      m.put("1", 100);
      m.put("2", 200);
      m.put("3", 300);
      m.put("4", 150);
      m.put("5", 110);
      m.put("6", 50);
      m.put("7", 90);
      m.put("8", 250);
      m.put("9", 350);
      m.put("10", 450);
      System.out.println("IdentityHashMap elements
"+ m);       System.out.println("Is value 100 in the Map? " + m.containsValue(100));    } }

輸出

IdentityHashMap elements
{2 = 200, 4 = 150, 9 = 350, 7 = 90, 10 = 450, 6 = 50, 5 = 110, 8 = 250, 1 = 100, 3 = 300}
Is value 100 in the Map? true

更新於: 25-6 月-2020

56 次瀏覽

開啟您的職業生涯

獲得認證,透過課程學習

開始
廣告