Método IdentityHashMap put() en Java

El método java.util.IdentityHashMap.put() de IdentityHashMap se utiliza para insertar una asignación en un mapa. Esto significa que podemos insertar una clave específica y el valor que está mapeando, en un mapa particular. Si se pasa una clave existente, el valor anterior se reemplaza por el nuevo valor. Si se pasa un nuevo par, entonces el par se inserta como un todo.

Sintaxis:

Identity_Hash_Map.put(key, value)

Parámetros: El método toma dos parámetros, ambos son del tipo Objeto del IdentityHashMap.

  • clave: Esto se refiere al elemento clave que debe insertarse en el Mapa para el mapeo.
  • valor: Esto se refiere al valor al que se asignaría la clave anterior.

Valor devuelto: si se pasa una clave existente, se devuelve el valor anterior. Si se pasa un par nuevo, se devuelve NULL.

Los siguientes programas se utilizan para ilustrar el funcionamiento del método java.util.IdentityHashMap.put():
Programa 1: al pasar una clave existente.

// Java code to illustrate the put() method
import java.util.*;
  
public class Identity_Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty IdentityHashMap
        Map<Integer, String> identity_hash = new IdentityHashMap<Integer, String>();
  
        // Mapping string values to int keys
        identity_hash.put(10, "Geeks");
        identity_hash.put(15, "4");
        identity_hash.put(20, "Geeks");
        identity_hash.put(25, "Welcomes");
        identity_hash.put(30, "You");
  
        // Displaying the IdentityHashMap
        System.out.println("Initial Mappings are: " + identity_hash);
  
        // Inserting existing key along with new value
        String returned_value = (String)identity_hash.put(20, "All");
  
        // Verifying the returned value
        System.out.println("Returned value is: " + returned_value);
  
        // Displayin the new map
        System.out.println("New map is: " + identity_hash);
    }
}
Producción:

Initial Mappings are: {10=Geeks, 30=You, 20=Geeks, 25=Welcomes, 15=4}
Returned value is: Geeks
New map is: {10=Geeks, 30=You, 20=All, 25=Welcomes, 15=4}

Programa 2: Al pasar una nueva clave.

// Java code to illustrate the put() method
import java.util.*;
  
public class Identity_Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty IdentityHashMap
        Map<Integer, String> identity_hash = new IdentityHashMap<Integer, String>();
  
        // Mapping string values to int keys
        identity_hash.put(10, "Geeks");
        identity_hash.put(15, "4");
        identity_hash.put(20, "Geeks");
        identity_hash.put(25, "Welcomes");
        identity_hash.put(30, "You");
  
        // Displaying the IdentityHashMap
        System.out.println("Initial Mappings are: " + identity_hash);
  
        // Inserting existing key along with new value
        String returned_value = (String)identity_hash.put(50, "All");
  
        // Verifying the returned value
        System.out.println("Returned value is: " + returned_value);
  
        // Displayin the new map
        System.out.println("New map is: " + identity_hash);
    }
}
Producción:

Initial Mappings are: {10=Geeks, 30=You, 20=Geeks, 25=Welcomes, 15=4}
Returned value is: null
New map is: {10=Geeks, 30=You, 20=Geeks, 25=Welcomes, 15=4, 50=All}

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 *