目录

boolean containsKey(Object key)

描述 (Description)

如果此映射包含指定键的映射,则containsKey(Object key)方法用于返回“true”。

声明 (Declaration)

以下是java.util.TreeMap.containsKey()方法的声明。

public boolean containsKey(Object key)

参数 (Parameters)

key - 这是要在此地图中进行测试的密钥。

返回值 (Return Value)

如果此映射包含指定键的映射,则方法调用返回true。

异常 (Exception)

  • ClassCastException - 如果无法将指定的键与当前映射中的键进行比较,则抛出此异常。

  • NullPointerException - 如果指定的键为null并且此映射使用自然排序,或者其比较器不允许空键,则抛出此异常。

例子 (Example)

以下示例显示了java.util.TreeMap.containsKey()方法的用法。

package com.iowiki;
import java.util.*;
public class TreeMapDemo {
   public static void main(String[] args) {
      // creating tree map 
      NavigableMap<Integer, String> treemap = new TreeMap<Integer, String>();
      // 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 key value 6");
      System.out.println("Value for key 6 exists: "+ treemap.containsKey(6));
   }    
}

让我们编译并运行上面的程序,这将产生以下结果。

Checking key value 6
Value for key 6 exists: true
↑回到顶部↑
WIKI教程 @2018