Share on Google+Share on Google+

Example of remove(Object key) method of HashMap.

Advertisement
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); } }

Output :

HashMap : {1=AAAA, 2=BBBB, 3=CCCC, 4=DDDD, 5=EE}

HashMap : {1=AAAA, 2=BBBB, 3=CCCC, 5=EE}

Advertisements

Posted on: April 6, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.