Share on Google+Share on Google+

Java hashmap clear() method example.

Advertisement
Java hashmap clear() method example.

Java hashmap clear() method example.

This tutorial is based on clear() method of java HashMap class. It removes all values from HashMap.

Code

HashMapRemoveAll.java

package net.roseindia.java;

import java.util.HashMap;

public class HashMapRemoveAll{

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 clear method */

System.out.println("HashMap : " + obMap);

/*Remove all values from HashMap*/

obMap.clear();

/*Display Element after clear method */

System.out.println("HashMap : "+obMap);m } }

Output :

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

HashMap : {}

Advertisements

Advertisement

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.