Friday, September 9, 2011

HashMap supports null key and null values

HashMap supports one null key and more than one null values. If we used more than one null keys then as HashMap support unique key then previous null key is override by next null key and only next null key and value is shown.


//HashMap.java (Test for null key and value)
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMap
{
public static void main(String args[])
{
try
{
HashMap hashMap = new HashMap();
hashMap.put(null, 2); //Put a null key.
hashMap.put(2, null); //Put a null value.
hashMap.put(null, 1); //Put again a null key.
hashMap.put(3, 3);
hashMap.put(4, 89);

Set set = hashMap.entrySet();
Iterator it = set.iterator();
while (it.hasNext())
{
Map.Entry me = (Map.Entry) it.next();
System.out.println("HashMap key " + me.getKey() + " and it's value is " + me.getValue());
}
}
catch (Exception e)
{
// TODO: handle exception
System.out.println("HashTable don't support " + e.getMessage() + " key and value.");
}
}
}

Output =>
HashMap key 2 and it's value is null
HashMap key 4 and it's value is 89
HashMap key null and it's value is 1
HashMap key 3 and it's value is 3


HashMap Examples:
1)       Simple HashMap example :
package com.example.hashmap;

import java.util.HashMap;

public class HashMapExample {
      public static void main(String[] args) {
            try {
                  HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
                  hashMap.put(1, 11);
                  hashMap.put(2, 22);
                  hashMap.put(3, 33);
                  hashMap.put(4, 44);
                  hashMap.put(5, 55);

                  for (Object name : hashMap.keySet()) {
                        System.out.println("Name: " + name + " Age= "
                                    + hashMap.get(name));
                  }
            } catch (Exception e) {
                  System.out.println("HashMap don't support " + e.getMessage()
                              + " key and value.");
            }
      }

}
OUTPUT:

Name: 1 Age= 11
Name: 2 Age= 22
Name: 3 Age= 33
Name: 4 Age= 44
Name: 5 Age= 55
2)       Has map supports only one null key. If we will used more than one null keys then the last one is override over the previous null key.
//One null key applied in HashMap example.
package com.example.hashmap;

import java.util.HashMap;

public class HashMapExample {
      public static void main(String[] args) {
            try {
                  HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
                  hashMap.put(null, 11);//HasMap supports one null key.
                  hashMap.put(2, 22);
                  hashMap.put(3, 33);
                  hashMap.put(4, 44);
                  hashMap.put(5, 55);

                  for (Object name : hashMap.keySet()) {
                        System.out.println("Name: " + name + " Age= "
                                    + hashMap.get(name));
                  }
            } catch (Exception e) {
                  System.out.println("HashMap don't support " + e.getMessage()
                              + " key and value.");
            }
      }

}

OUTPUT:
Name: null Age= 11
Name: 2 Age= 22
Name: 3 Age= 33
Name: 4 Age= 44
Name: 5 Age= 55
//Multiple null keys applied in HashMap example.
package com.example.hashmap;

import java.util.HashMap;

public class HashMapExample {
      public static void main(String[] args) {
            try {
                  HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
                  hashMap.put(null, 11);//First null key.
                  hashMap.put(2, 22);
                  hashMap.put(null, 33);//Second null key. First null key will be override by this (second null key.)
                  hashMap.put(4, 44);
                  hashMap.put(5, 55);

                  for (Object name : hashMap.keySet()) {
                        System.out.println("Name: " + name + " Age= "
                                    + hashMap.get(name));
                  }
            } catch (Exception e) {
                  System.out.println("HashMap don't support " + e.getMessage()
                              + " key and value.");
            }
      }

}
OUTPUT:
Name: null Age= 33
Name: 2 Age= 22
Name: 4 Age= 44
Name: 5 Age= 55


3)       HasMapp supports multiple null values.

//Multiple null values applied in HashMap example.
package com.example.hashmap;

import java.util.HashMap;

public class HashMapExample {
      public static void main(String[] args) {
            try {
                  HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
                  hashMap.put(1, null);//First null value.
                  hashMap.put(2, 22);
                  hashMap.put(3, null);//Second null value.
                  hashMap.put(4, 44);
                  hashMap.put(5, 55);

                  for (Object name : hashMap.keySet()) {
                        System.out.println("Name: " + name + " Age= "
                                    + hashMap.get(name));
                  }
            } catch (Exception e) {
                  System.out.println("HashMap don't support " + e.getMessage()
                              + " key and value.");
            }
      }

}
OUTPUT:
Name: 1 Age= null
Name: 2 Age= 22
Name: 3 Age= null
Name: 4 Age= 44
Name: 5 Age= 55

No comments:

Post a Comment