📜  检查Java HashMap 中是否存在特定值

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

检查Java HashMap 中是否存在特定值

Java HashMap是 Map 接口的一个实现,它将一个值映射到一个键,它本质上形成了一个关联对,其中我们可以根据键调用一个值。 Java HashMap 提供了很多优点,例如允许 Key 和 Value 使用不同的数据类型,这使得这种数据结构更具包容性和通用性。它还允许 Key 和 Value 为 null,因为形成一对的两个中的一个不为 null。

检查Java HashMap 中特定值是否存在的不同方法是:

  1. 使用 HashMap 类的内置 containsValue() 方法
  2. 从 HashMap 的条目创建映射,然后遍历值
  3. 从 HashMap 的值创建一个 ArrayList,然后遍历这个列表

方法一:

这种方法提供了一种相当简单和有效的方法来使用containsValue()预定义方法检查 HashMap 中值的存在,该方法返回一个布尔值。

句法:

Hash_Map.containsValue(Object Value)

参数:该方法只接受一个 Object 类型的参数Value ,并引用映射应该由映射内的任何键检查其映射的值。

返回值:如果检测到值的映射,则该方法返回布尔值 true,否则返回 false。

算法 :

  1. 声明一个返回类型为 boolean 的方法来检查值是否存在。
  2. 初始化一个 HashMap,描述 Key 和 Values 的数据类型。
  3. 使用 HashMap 类的内置 put() 方法用键值对填充此 HashMap。
  4. 声明一个布尔变量来存储 containsValue() 方法的结果。

代码 :

Java
// java program to check if a particular
// value exists in a HashMap
 
import java.util.*;
 
class GFG {
 
    // declaring the method to return
    // if the value is present or not
    // the parameter valueToBeChecked
    // represents the value to be checked
    boolean checkForValue(int valueToBeChecked)
    {
 
        // initializing the HashMap
        HashMap hashMap = new HashMap<>();
 
        // filling the HashMap with
        // key value pairs
        hashMap.put("key1", 1);
        hashMap.put("key2", 2);
        hashMap.put("key3", 3);
        hashMap.put("key4", 4);
 
        // declaring the variable to store
        // the result
        // calling the containsValue() method
        boolean result
            = hashMap.containsValue(valueToBeChecked);
 
        // returning the result
        return result;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // instantiating the class
        GFG ob = new GFG();
 
        // displaying and calling the
        // checkForValue() method
        System.out.println(ob.checkForValue(10));
    }
}


Java
// java program to check if a particular
// value exists in HashMap
 
import java.util.*;
 
class GFG {
 
    // declaring the method
    // the parameter is the variable which
    // stores the value to be checked
    boolean checkForValue(int valueToBeChecked)
    {
 
        // initializing the HashMap
        HashMap hashMap = new HashMap<>();
 
        // filling up the HashMap with
        // Key-Value pairs
        hashMap.put("key1", 1);
        hashMap.put("key2", 2);
        hashMap.put("key3", 3);
        hashMap.put("key4", 4);
 
        // for each loop
        // all the entries in the HashMap are
        // stored in the Map
        for (Map.Entry mapEntries :
             hashMap.entrySet()) {
 
            // fetching the values of the HashMap
            // one at a time and comparing with
            // the value to be checked
            if (mapEntries.getValue() == valueToBeChecked)
                return true;
        }
        return false;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // instantiating the class
        GFG ob = new GFG();
 
        // displaying and calling the
        // checkForValue() method
        System.out.println(ob.checkForValue(2));
    }
}


Java
// java program to check if a particular
// key exists in a HashMap
 
import java.util.*;
 
class GFG {
 
    // declaring the method
    // the parameter specifies the value to
    // be checked
    boolean checkForValue(int valueToBeChecked)
    {
 
        // initializing the HashMap
        HashMap hashMap = new HashMap<>();
 
        // filling up the HashMap with
        // Key-Value pairs
        hashMap.put("key1", 1);
        hashMap.put("key2", 2);
        hashMap.put("key3", 3);
        hashMap.put("key4", 4);
 
        // declaring an ArrayList of type
        // Integer from the values of the
        // HashMap using the predefined
        // values() method of the HashMap class
        ArrayList listOfValues
            = new ArrayList<>(hashMap.values());
 
        // declaring the iterator
        Iterator itr = listOfValues.iterator();
 
        // iterating through the list
        while (itr.hasNext()) {
 
            // comparing each value with the
            // one specified
            if (itr.next() == valueToBeChecked)
                return true;
        }
        return false;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // instantiating the class
        GFG ob = new GFG();
 
        // displaying and calling the
        // checkForValue() method
        System.out.println(ob.checkForValue(0));
    }
}


输出
false

方法二:

使用这种方法,我们从 HashMap 的所有条目、包括的键和值创建一个映射,然后只遍历值以检查特定的值。

算法 :

  1. 重复第 1 步到第 3 步,如创建 HashMap 的第一种方法所述。
  2. 然后使用 for each 循环和预定义的entrySet()创建一个 Map HashMap 的方法。
  3. 使用 Map 类的预定义getValue()方法遍历 Map 中的值。
  4. 在每次迭代时,将 Value 与指定的 Value 进行比较并返回相应的结果。

代码 :

Java

// java program to check if a particular
// value exists in HashMap
 
import java.util.*;
 
class GFG {
 
    // declaring the method
    // the parameter is the variable which
    // stores the value to be checked
    boolean checkForValue(int valueToBeChecked)
    {
 
        // initializing the HashMap
        HashMap hashMap = new HashMap<>();
 
        // filling up the HashMap with
        // Key-Value pairs
        hashMap.put("key1", 1);
        hashMap.put("key2", 2);
        hashMap.put("key3", 3);
        hashMap.put("key4", 4);
 
        // for each loop
        // all the entries in the HashMap are
        // stored in the Map
        for (Map.Entry mapEntries :
             hashMap.entrySet()) {
 
            // fetching the values of the HashMap
            // one at a time and comparing with
            // the value to be checked
            if (mapEntries.getValue() == valueToBeChecked)
                return true;
        }
        return false;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // instantiating the class
        GFG ob = new GFG();
 
        // displaying and calling the
        // checkForValue() method
        System.out.println(ob.checkForValue(2));
    }
}
输出
true

方法三:

在第三种方法下,我们从 HashMap 的值创建一个 ArrayList 并遍历整个列表以检查提到的特定值。

算法 :

  1. 重复步骤 1 到 3 以创建 HashMap。
  2. 接下来,创建一个与 HashMap 中的值具有相同数据类型的 ArrayList。
  3. 在下一步中,我们声明一个迭代器来遍历整个 ArrayList,直到找到特定值。
  4. 使用 while 循环遍历存储在 ArrayList 中的值,并在每次迭代中使用指定的值进行检查。
  5. 将相应的结果作为布尔值返回。

代码 :

Java

// java program to check if a particular
// key exists in a HashMap
 
import java.util.*;
 
class GFG {
 
    // declaring the method
    // the parameter specifies the value to
    // be checked
    boolean checkForValue(int valueToBeChecked)
    {
 
        // initializing the HashMap
        HashMap hashMap = new HashMap<>();
 
        // filling up the HashMap with
        // Key-Value pairs
        hashMap.put("key1", 1);
        hashMap.put("key2", 2);
        hashMap.put("key3", 3);
        hashMap.put("key4", 4);
 
        // declaring an ArrayList of type
        // Integer from the values of the
        // HashMap using the predefined
        // values() method of the HashMap class
        ArrayList listOfValues
            = new ArrayList<>(hashMap.values());
 
        // declaring the iterator
        Iterator itr = listOfValues.iterator();
 
        // iterating through the list
        while (itr.hasNext()) {
 
            // comparing each value with the
            // one specified
            if (itr.next() == valueToBeChecked)
                return true;
        }
        return false;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // instantiating the class
        GFG ob = new GFG();
 
        // displaying and calling the
        // checkForValue() method
        System.out.println(ob.checkForValue(0));
    }
}
输出
false