Example of remove(Object key) method of HashMap.
Example of remove(Object key) method of HashMap.In this tutorial, you will see the use of remove method of HashMap class in java. It removes a value of specific key from HashMap.
Code:
HashMapRemove.java
package net.roseindia.java; import java.util.HashMap; public class HashMapRemove { public static void main(String[] arr) { /* Create a object of HfasHMap */ HashMap<Integer, String> obMap = new HashMap<Integer, String>(); /* Add values into HashMap */ obMap.put(new Integer(1), "AAAA"); obMap.put(new Integer(2), "BBBB"); obMap.put(new Integer(3), "CCCC"); obMap.put(new Integer(4), "DDDD"); obMap.put(new Integer(5), "EE"); /*Display Element before remove method */ System.out.println("HashMap : " + obMap); /*Remove values from HashMap*/ obMap.remove(4); /*Display Element after remove method */ System.out.println("HashMap : "+obMap); } } |
HashMap : {1=AAAA, 2=BBBB, 3=CCCC, 4=DDDD, 5=EE} HashMap : {1=AAAA, 2=BBBB, 3=CCCC, 5=EE} |