Java hashmap clear() method example.


 

Java hashmap clear() method example.

Java hashmap clear() method example.

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 : {}

Ads