📜  如何检查Java中的HashMap中是否存在键

📅  最后修改于: 2022-05-13 01:55:13.071000             🧑  作者: Mango

如何检查Java中的HashMap中是否存在键

给定一个 HashMap 和Java中的一个键,任务是检查该键是否存在于 HashMap 中。

例子:

Input: HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks}, key = 2
Output: true

Input: HashMap: {1=G, 2=e, 3=e, 4=k, 5=s}, key = 10
Output: false
  1. 使用迭代器(效率不高)
    1. 获取 HashMap 和密钥
    2. 使用 HashMap.iterate() 方法创建一个迭代器来迭代 HashMap。
    3. 使用 Iterator.hasNext() 方法迭代 HashMap。
    4. 迭代时,检查该迭代的键是否等于指定的键。 Map 的入口键可以通过 entry.getKey() 方法获取。
    5. 如果键匹配,则将标志设置为真。
    6. 迭代后的标志值,包含结果。

    下面是上述方法的实现:

    方案一:

    // Java program to check if a key exists
    // in a HashMap or not
      
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // Create a HashMap
            HashMap
                map = new HashMap<>();
      
            // Populate the HashMap
            map.put(1, "Geeks");
            map.put(2, "ForGeeks");
            map.put(3, "GeeksForGeeks");
      
            // Get the key to be removed
            int keyToBeChecked = 2;
      
            // Print the initial HashMap
            System.out.println("HashMap: "
                               + map);
      
            // Get the iterator over the HashMap
            Iterator >
                iterator = map.entrySet().iterator();
      
            // flag to store result
            boolean isKeyPresent = false;
      
            // Iterate over the HashMap
            while (iterator.hasNext()) {
      
                // Get the entry at this iteration
                Map.Entry
                    entry
                    = iterator.next();
      
                // Check if this key is the required key
                if (keyToBeChecked == entry.getKey()) {
      
                    isKeyPresent = true;
                }
            }
      
            // Print the result
            System.out.println("Does key "
                               + keyToBeChecked
                               + " exists: "
                               + isKeyPresent);
        }
    }
    
    输出:
    HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks}
    Does key 2 exists: true
    

    程序 2:说明为什么不建议使用此方法。如果 HashMap 包含空值,则此方法将抛出 NullPointerException。这使得该方法成为不建议使用的方法。

    // Java program to check if a key exists
    // in a HashMap or not
      
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            try {
                // Create a HashMap
                HashMap
                    map = new HashMap<>();
      
                // Populate the HashMap
                map.put(1, "Geeks");
                map.put(2, "ForGeeks");
                map.put(null, "GeeksForGeeks");
      
                // Get the key to be removed
                int keyToBeChecked = 2;
      
                // Print the initial HashMap
                System.out.println("HashMap: "
                                   + map);
      
                // Get the iterator over the HashMap
                Iterator >
                    iterator = map.entrySet().iterator();
      
                // flag to store result
                boolean isKeyPresent = false;
      
                // Iterate over the HashMap
                while (iterator.hasNext()) {
      
                    // Get the entry at this iteration
                    Map.Entry
                        entry
                        = iterator.next();
      
                    // Check if this key is the required key
                    if (keyToBeChecked == entry.getKey()) {
      
                        isKeyPresent = true;
                    }
                }
      
                // Print the result
                System.out.println("Does key "
                                   + keyToBeChecked
                                   + " exists: "
                                   + isKeyPresent);
            }
            catch (Exception e) {
                System.out.println(e);
            }
        }
    }
    
    输出:
    HashMap: {null=GeeksForGeeks, 1=Geeks, 2=ForGeeks}
    java.lang.NullPointerException
    
  2. 使用 HashMap.containsKey 方法(高效)
    1. 获取 HashMap 和密钥
    2. 使用 HashMap.containsKey() 方法检查键是否存在于 HashMap 中。如果密钥存在,则将标志设置为真。
    3. 标志值,包含结果。

    下面是上述方法的实现:

    方案一:

    // Java program to check if a key exists
    // in a HashMap or not
      
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // Create a HashMap
            HashMap
                map = new HashMap<>();
      
            // Populate the HashMap
            map.put(1, "Geeks");
            map.put(2, "ForGeeks");
            map.put(null, "GeeksForGeeks");
      
            // Get the key to be removed
            int keyToBeChecked = 2;
      
            // Print the initial HashMap
            System.out.println("HashMap: "
                               + map);
      
            // Check is key exists in the Map
            boolean isKeyPresent = map.containsKey(keyToBeChecked);
      
            // Print the result
            System.out.println("Does key "
                               + keyToBeChecked
                               + " exists: "
                               + isKeyPresent);
        }
    }
    
    输出:
    HashMap: {null=GeeksForGeeks, 1=Geeks, 2=ForGeeks}
    Does key 2 exists: true