This is a quick example on how to get default values in hashMaps and other java objacts.

Here is the source code:

    public class App {
        public static void main(String[] args) {
            System.out.println("Hello World, Default Values In HashMap Example" );

            Map<String, String> myMap = new HashMap<String, String>();

            myMap.put("SABRAIN", "KID");
            myMap.put("JESSCIA", "KID");

            Iterator iterator = myMap.entrySet().iterator();

             *   This will show all the keys and values in my hashMap
            while (iterator.hasNext()) {
                Map.Entry mapEntry = (Map.Entry);
                System.out.println("The key is: " + mapEntry.getKey()
                        + ",value is :" + mapEntry.getValue());

             * Now lets look for someone not in the map and setup a default value



You need to add this to your POM


checkout the project from github.

git clone
cd DefaultValuesInHashMap
mvn package
cd target
java -jar DefaultValuesInHashMap.jar

If you have any questions or comments please email me at or checkout my web site


01 July 2013