Java HashMap Example

Java HashMap example.
This Java HashMap example describes the basic operations performed on the HashMap.
import java.util.HashMap;
import java.util.Iterator;
public class HashMapExample{
    public static void main(String args[]){
        // constructs a new empty HashMap with default initial capacity        
        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
        To specify initial capacity, use following constructor        
        HashMap<String, Integer> hashMap = new HashMap<String, Integer>(100);
        To create HashMap from map use following constructor        
        HashMap<String, Integer> hashMap = new HashMap<String, Integer>(Map myMap);
        hashMap.put("One", new Integer(1)); // adding value into HashMap
        hashMap.put("Two", new Integer(2));
        hashMap.put("Three", new Integer(3));
        IMPORTANT : We CAN NOT add primitives to the HashMap. We have to wrap it into
        one of the wrapper classes before adding.
        To copy all key - value pairs from any Map to HashMap use putAll method.
        Signature of putAll method is,        
        void putAll(Map m)
        //get number of keys present in the HashMap        
        System.out.println("HashMap contains " + hashMap.size() + " key value pairs.");
        To check whether HashMap is empty or not, use isEmpty() method.        
        isEmpty() returns true is HashMap is empty, otherwise false.

        Finding particular value from the HashMap:
        HashMap's containsValue method returns boolean depending upon
        the presence of the value in given HashMap
        Signature of the containsValue method is,        
        boolean containsValue(Object value)
        if(hashMap.containsValue(new Integer(1))){
            System.out.println("HashMap contains 1 as value");
            System.out.println("HashMap does not contain 1 as value");
        Finding particular Key from the HashMap:        
        HashMap's containsKey method returns boolean depending upon the
        Presence of the key in given HashMap
        Signature of the method is,        
        boolean containsKey(Object key)
        if( hashMap.containsKey("One") ){
            System.out.println("HashMap contains One as key");
            System.out.println("HashMap does not contain One as value");
        Use get method of HashMap to get value mapped to particular key.        
        Signature of the get method is,        
        Object get(Object key)        
        Integer one = (Integer) hashMap.get("One");        
        System.out.println("Value mapped with key \"One\" is " + one);
        IMPORTANT:  get method returns Object, so we need to downcast it.
        To get all keys stored in HashMap use keySet method        
        Signature of the keysSet method is,        
        Set keySet()
        System.out.println("Retrieving all keys from the HashMap");        
        Iterator iterator = hashMap.keySet().iterator();
        To get all values stored in HashMap use entrySet() method.
        Signature of the entrySet() method is,        
        Set entrySet()
        System.out.println("Retrieving all values from the HashMap");
        iterator = hashMap.entrySet().iterator();
        To remove particular key - value pair from the HashMap use remove method.        
        Signature of remove method is,        
        Object remove(Object key)        
        This method returns value that was mapped to the given key,
        otherwise null if mapping not found.
        System.out.println( hashMap.remove("One") + " is removed from the HashMap.");

OUTPUT of the above given Java HashMap Example would be :
HashMap contains 3 key value pairs.
HashMap contains 1 as value
HashMap contains One as key
Value mapped with key "One" is 1
Retrieving all keys from the HashMap
Retrieving all values from the HashMap
1 is removed from the HashMap.

Note that HashMap makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added. If you do find yourself having to iterate through a HashMap, remember this little caveat:
“This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.”HashMap API

If you need consistent ordering, you can use LinkedHashMap (for insertion/access order).LinkedHashMap will iterate in the order in which the entries were put into the map.



About kaanmutlu

Software Developer - Computer Engineer from Istanbul, Turkey
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s