1

Here i have sample of code for Hash Table and Concurrent Hash Map which does not allow null key as a Object But Both are Synchronized.

  public class Example
  {
public static void main(String[] args)
{
    ConcurrentHashMap<String,String> premiumPhone = 
                           new ConcurrentHashMap<String,String>();
    premiumPhone.put("Apple", "iPhone");
    premiumPhone.put("HTC", "HTC one");
    premiumPhone.put(null,"S5");

    Iterator iterator = premiumPhone.keySet().iterator();

    while (iterator.hasNext())
    {
        System.out.println(premiumPhone.get(iterator.next()));
        premiumPhone.put("Sony", "Xperia Z");
    }

}
}
Santosh
  • 111
  • 2
  • 10

1 Answers1

2

From the Javadoc for ConcurrentHashMap

Like Hashtable but unlike HashMap, this class does not allow null to be used as a key or value.

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html

Also, take a look at this: https://stackoverflow.com/a/9298113/2227788

Community
  • 1
  • 1
Aamir Khan
  • 2,945
  • 2
  • 25
  • 32
  • why serialized Object giving a null pointer exception. – Santosh Apr 26 '16 at 12:51
  • @Santosh you are serializing an Object, you are passing a reference as the key. – Peter Lawrey Apr 26 '16 at 12:57
  • @Santosh I don't understand what you mean.. You're not serializing an Object, you are trying to put a null key into a ConcurrentHashMap which is not possible, as referenced in the Javadoc I linked. – Aamir Khan Apr 26 '16 at 13:44