Remove an element from IdentityHashMap in Java

Java 8Object Oriented ProgrammingProgramming

Use the remove() method to remove an element from IdentityHashMap.

Let us create IdentityHashMap and add some elements

Map<String, Integer> m = new IdentityHashMap<String, Integer>();
m.put("1", 100);
m.put("2", 200);
m.put("3", 300);
m.put("4", 150);
m.put("5", 110);
m.put("6", 50);
m.put("7", 90);
m.put("8", 250);
m.put("9", 350);
m.put("10", 450);

Let’s say you need to remove 2 from the Map. For that, use the remove() method

m.remove("2");

The following is an example to remove an element from Java IdentityHashMap

Example

 Live Demo

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      Map<String, Integer> m = new IdentityHashMap<String, Integer>();
      m.put("1", 100);
      m.put("2", 200);
      m.put("3", 300);
      m.put("4", 150);
      m.put("5", 110);
      m.put("6", 50);
      m.put("7", 90);
      m.put("8", 250);
      m.put("9", 350);
      m.put("10", 450);
      System.out.println("IdentityHashMap elements\n"+ m);
      m.remove("2");
      System.out.println("\nUpdated IdentityHashMap elements\n"+ m);
   }
}

The output is as follows

IdentityHashMap elements
{2=200, 4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300}
Updated IdentityHashMap elements
{4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300}
raja
Published on 17-Jan-2019 08:20:19
Advertisements