Saturday, May 10, 2014

About Hashmap in Java Collections

Hashmap: Hashmap is collection class where you can store Objects and playing with it.
In Hashmap objects are stored in Key, Value pair.
Keys are also objects, Hashmap stores the keys based on Object hashing.
The better your hashCode implementation the better you can store Hashmap.

Hashmap accepts only one NULL object as key.

NOTE: When you try to add two null keys to hashmap it takes only the last added entry.


package com.sat.java.collections;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author Satya
 */
public class TestJava {

    /**
     * @param args
     */
    public static void main(String[] args) {
       
        Map map = new HashMap();
       
        map.put(null, "X");
        map.put(null, "Y");
        //Iterating over entrySet
        for(Entry ent: map.entrySet()){
            System.out.println(ent.getKey() + " , " + ent.getValue());
        }

    }

}


Output of this program:
null , Y


No comments: