Java – How to clear HashMap

All Elements in HashMap can be removed by clear() method. Clear() method in HashMap clear HashMap object and size of the HashMap will become 0

Example of clear in HashMap

import java.util.HashMap;

public class HashMapClear {

    public static void main(String[] args) {

        HashMap<Object,String> hm=new HashMap<Object,String>();

        hm.put(new Integer(2), "Two");
        hm.put(new Integer(1), "One");
        hm.put(new Integer(3), "Three");
        hm.put(new Integer(4), "Four");

        hm.clear();

        System.out.println("Size of hashmap :"+hm.size());
    }
}

Output

Size of hashmap :0

Tags:

Bookmark  

 

Leave a Reply

Security Code:

 

  Random Post