Home > Java > JavaBase > How to determine whether the specified key exists in the map collection object in java

How to determine whether the specified key exists in the map collection object in java

王林
Release: 2019-11-21 15:19:48
Original
3880 people have browsed it

How to determine whether the specified key exists in the map collection object in java

This method determines whether the Map collection object contains the specified key name. If the Map collection contains the specified key name, returns true, otherwise returns false.

Syntax:

containsKey(Object key)
Copy after login

Judgment method:

First define a map object, then add objects to the collection through the "put()" method, and then define a The specified key value can be judged through the "map.containsKey()" method.

Example:

public static void main(String[] args) {
  Map map = new HashMap();       //定义Map对象
  map.put("apple", "新鲜的苹果");      //向集合中添加对象
  map.put("computer", "配置优良的计算机");
  map.put("book", "堆积成山的图书");
  map.put("time", new Date()); 
  String key = "book"; 
  boolean contains = map.containsKey(key);    //判断是否包含指定的键值
  if (contains) {         //如果条件为真
   System.out.println("在Map集合中包含键名" + key); //输出信息
  } else {
   System.out.println("在Map集合中不包含键名" + key);
  }
}
Copy after login

Recommended tutorial: Getting started with java development

The above is the detailed content of How to determine whether the specified key exists in the map collection object in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template