Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

VERY Strange problem with HashTable and HashMap.

Thread view: 
rlnasr@gmail.com - 19 Jun 2007 00:17 GMT
Check this out...

If you execute this code you would think that you get 4 entries in the
Hashtable, however only three entries are present.  I am using jdk
1.5.0_09.  I observed the same kind of bug with Hashmap as well.

What seems to be happening is the final "put" for the "FAX" key is
overwriting the first "DESC" entry.  Very very strange.  Does anyone
know what may be happening here?

java.util.Hashtable hashtable = new java.util.Hashtable();

hashtable.put("DESC", Boolean.FALSE);
hashtable.put("ATT", Boolean.FALSE);
hashtable.put("POST", Boolean.FALSE);
hashtable.put("FAX", Boolean.FALSE);

return hashtable;
Stefan Ram - 19 Jun 2007 00:58 GMT
>return hashtable;

 This prints

{FAX=false, DESC=false, POST=false, ATT=false}

 here. Consider posting a SSCCE.
Mark Rafn - 19 Jun 2007 01:00 GMT
>If you execute this code you would think that you get 4 entries in the
>Hashtable, however only three entries are present.  I am using jdk
>1.5.0_09.  I observed the same kind of bug with Hashmap as well.

You're doing something else wrong.  Post a complete (simple, self-contained)
example and we'll tell you what's going on.

>What seems to be happening is the final "put" for the "FAX" key is
>overwriting the first "DESC" entry.  Very very strange.  Does anyone
>know what may be happening here?

You're not telling us the whole story.  Something else in your code is
confusing you.

>java.util.Hashtable hashtable = new java.util.Hashtable();
>hashtable.put("DESC", Boolean.FALSE);
>hashtable.put("ATT", Boolean.FALSE);
>hashtable.put("POST", Boolean.FALSE);
>hashtable.put("FAX", Boolean.FALSE);
>return hashtable;

I see no such problem.

import java.util.Hashtable;
public class Test {
   public static Hashtable getTable() {
       Hashtable hashtable = new Hashtable();
       hashtable.put("DESC", Boolean.FALSE);
       hashtable.put("ATT", Boolean.FALSE);
       hashtable.put("POST", Boolean.FALSE);
       hashtable.put("FAX", Boolean.FALSE);
       return hashtable;
   }

   public static void main(String[] args) {
       Hashtable ht = getTable();
       System.out.println("number of entries: " + ht.size());
   }
}

$ java -version
java version "1.5.0_11"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode)

$ java -cp . Test
number of entries: 4
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>
oh_maan - 19 Jun 2007 08:04 GMT
On Jun 19, 4:17 am, rln...@gmail.com wrote:
> Check this out...
>
[quoted text clipped - 14 lines]
>
> return hashtable;

I guess you attached a debugger to examine the contents of the
hashtable and found only 3 entries there. What might have happened is
that there was a collision between 2 of your keys and thus one of the
colliding key was linearly chained with the other one. If you examine
the key in the hash table closely you will find a "next" element which
holds the key that collided with it. If this is not the case, then
there is some bug in your code. Please post the whole code.

Regards..


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.