Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird Hashtable problem

Status
Not open for further replies.

Swamphen

Programmer
Jul 17, 2001
84
0
0
IT
When I try to initialize my hashtable by doing:

///////////start code////////////////////////

Hashtable hashtable = new Hashtable();

hashtable.put("H",new Integer(0));
hashtable.put("C",new Integer(0));
hashtable.put("0",new Integer(0));
hashtable.put("F",new Integer(0));
hashtable.put("B",new Integer(0));
hashtable.put("N",new Integer(0));
hashtable.put("S",new Integer(0));

/////////Stop code///////////////////////

then only the 3 or 4 last hashtable key,object pairs are saved and the first 4 or 3 key,object pairs are overwritten.

Did I sleep to little? Or is this truly a strange behavior?

Thanks a lot,

Swamphen

 
I tested your code, and can see nothing wrong with the behaviour ...

Code:
public class Test {


	public static void main(String args[]) throws Exception {
		Hashtable hashtable = new Hashtable();

		int i = 0;
		hashtable.put("H",new Integer(i++));
		hashtable.put("C",new Integer(i++));
		hashtable.put("0",new Integer(i++));
		hashtable.put("F",new Integer(i++));
		hashtable.put("B",new Integer(i++));
		hashtable.put("N",new Integer(i++));
		hashtable.put("S",new Integer(i++));


		     for (Enumeration e = hashtable.keys() ; e.hasMoreElements() ;) {
				 Object key  = e.nextElement();
		         System.out.println(key +":" +hashtable.get(key));
		     }





	}
}

This prints out :

S:6
H:0
F:3
0:2
N:5
C:1
B:4

which is normal ...
 
OK. I did sleep to little. My apologies.

I got a NullPointerException when trying to get the Hashtable Object associated with a key equal to the first letter of some string.

And by watching the Hashtable variable in the debugger I got the impression that when constructing the Hashtable some of the old HashtableEntries were overwritten by the new HashtableEntries. But (of course!!) this was only the result of hash collisions after which the old entry and the new one resided together in the same bucket.

The real reason for the NullPointerException was (again, of course!!) the fact that two Strings with the same contents are still different Objects.

So I will have to use an Enumerator and match each key of the Hashtable to the "first-letter-string" with the "equals"-method.

Thanks for helping.

Swamphen







 
You do not need to use the enumerator in that way.

The general contract for hashcode includes

'If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.'

In your case if

public class Test2 {


public static void main(String args[]) throws Exception {
String s = "How are you today";
Hashtable hashtable = new Hashtable();
int i = 0;

hashtable.put("H",new Integer(i++));
hashtable.put("C",new Integer(i++));
hashtable.put("0",new Integer(i++));
hashtable.put("F",new Integer(i++));


System.out.println("H" +":" +hashtable.get(s.substring(0, 1)));
System.out.println("C" +":" +hashtable.get("C"));
}
}

works fine and produces

H:0
C:1

Cheers

Andrew
 
Well thats fine if you know what is in the hashtable !!!
 
What do you mean.

The OP implied that he would need to use the enumerator to match each key of the Hashtable to the "first-letter-string" with the "equals"-method, and then use the actual key to retrieve the object.

This is not true and completely pointless in any circumstances with any objects.

The contract for the hashcode means that if two objects are equal as indicated by the equals method then they must give the same hashcode and so be able to retrieve the same object from a hashtable. So the OP does not need to enumerate through the keys looking for one that equals() it, since by definition it will give the same hashcode and retrieve the same object as the key itself.

Lentil

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top