Método ConcurrentHashMap contains() en Java con ejemplos

El método contains() de Java.util.ConcurrentHashMap se usa para verificar si alguna clave se asigna al valor especificado en esta tabla. Es un método heredado para realizar una tarea en particular. La operación es similar al método containsValue() de ConcurrentHashMap.

Sintaxis:

ConcurrentHashMap.contains(Object Value)

Parámetros: el método toma solo un parámetro Valor de tipo Objeto y se refiere al valor cuyo mapeo se supone que debe verificar cualquier tecla dentro del mapa.

Valor devuelto: el método devuelve un valor booleano verdadero si se detecta la asignación del valor; de lo contrario, es falso.

Los siguientes programas se utilizan para ilustrar el funcionamiento del método ConcurrentHashMap.containsValue():

Programa 1: Asignación de valores de string a claves enteras.

// Java code to illustrate the contains() method
  
import java.util.*;
import java.util.concurrent.*;
  
public class ConcurrentHashMapDemo {
    public static void main(String[] args)
    {
  
        // Creating an empty HashMap
        ConcurrentHashMap<Integer, String>
            hash_map = new ConcurrentHashMap<Integer,
                                             String>();
  
        // Mapping string values to int keys
        hash_map.put(10, "Geeks");
        hash_map.put(15, "4");
        hash_map.put(20, "Geeks");
        hash_map.put(25, "Welcomes");
        hash_map.put(30, "You");
  
        // Displaying the ConcurrentHashMap
        System.out.println("Initial Mappings are: "
                           + hash_map);
  
        // Checking for the Value 'Geeks'
        System.out.println("Is the value 'Geeks' present? "
                           + hash_map.containsValue("Geeks"));
  
        // Checking for the Value 'World'
        System.out.println("Is the value 'World' present? "
                           + hash_map.containsValue("World"));
    }
}
Producción:

Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
Is the value 'Geeks' present? true
Is the value 'World' present? false

Programa 2: Asignación de valores enteros a claves de string.

// Java code to illustrate the contains() method
  
import java.util.*;
import java.util.concurrent.*;
  
public class ConcurrentHashMapDemo {
    public static void main(String[] args)
    {
  
        // Creating an empty ConcurrentHashMap
        ConcurrentHashMap<String, Integer>
            hash_map = new ConcurrentHashMap<String,
                                             Integer>();
  
        // Mapping int values to string keys
        hash_map.put("Geeks", 10);
        hash_map.put("4", 15);
        hash_map.put("Geeks", 20);
        hash_map.put("Welcomes", 25);
        hash_map.put("You", 30);
  
        // Displaying the ConcurrentHashMap
        System.out.println("Initial Mappings are: "
                           + hash_map);
  
        // Checking for the Value 'Geeks'
        System.out.println("Is the value 'Geeks' present? "
                           + hash_map.contains(25));
  
        // Checking for the Value 'World'
        System.out.println("Is the value 'World' present? "
                           + hash_map.contains(35));
    }
}
Producción:

Initial Mappings are: {4=15, Geeks=20, You=30, Welcomes=25}
Is the value 'Geeks' present? true
Is the value 'World' present? false

Nota: La misma operación se puede realizar con cualquier tipo de Mapping con variación y combinación de diferentes tipos de datos.

Publicación traducida automáticamente

Artículo escrito por Chinmoy Lenka y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *