Generally, we will use two methods to make judgments, namely:
1. Directly use the containsKey() method provided by java api;
2. Loop through and compare one by one.
(Recommended video tutorial: java video)
Specific code:
import java.util.Iterator; import java.util.Map; import com.google.common.collect.Maps; public class MapTest { public static void main(String[] args) { Map<String, String> map = Maps.newHashMap(); map.put("1", "1"); map.put("2", "2"); map.put("3", "3"); map.put("4", "4"); //方法1 System.out.println(map.containsKey("5")); //方法2 Iterator keys = map.keySet().iterator(); String key; while(keys.hasNext()){ key = (String) keys.next(); if ("1".equals(key)) { System.out.println("存在"); } } } }
Recommended tutorial: java entry program
The above is the detailed content of How to determine whether a certain key exists in the map collection in java. For more information, please follow other related articles on the PHP Chinese website!