Friday, September 9, 2011

HashTable don't support NULL key and value.


//HashTableExample.java (Test for null value in HashTable)
import java.util.Enumeration;

import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;

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

Enumeration e = hashtable.keys();
while (e.hasMoreElements())
{
Object o = (Object) e.nextElement();
System.out.println(o);
}
}
catch (Exception e)
{
// TODO: handle exception
System.out.println("HashTable don't support " + e.getMessage() + " value");
}
}
}

Output =>
HashTable don't support null value

//HashTableExample.java (Test for null key in HashTable)
import java.util.Enumeration;

import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;

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

Enumeration e = hashtable.keys();
while (e.hasMoreElements())
{
Object o = (Object) e.nextElement();
System.out.println(o);
}
}
catch (Exception e)
{
// TODO: handle exception
System.out.println("HashTable don't support " + e.getMessage() + " key");
}
}
}

Output =>
HashTable don't support null key

No comments:

Post a Comment