Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Strange goings on with Hashtable

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok we all know that Strings are immutable, and that Strings with the same characters share the same characters in memory.
So with hashtables, it's possible to do this :-

so that

all work.
But, I have found a case where this doesn't work, and I can't figure out why. Here is some code:-

So the code selects all weeks in the table, and builds an arraylist of beans for each week.
But if I say that the week is "17-JULY" and try and do a null object is returned. I thought that maybe the value was never stored, so I did an iterator on the keys and the week existed! I also iterated on the values and the object was there also!
I also did :-

The only way I could get the code above to return true was to pass in the original string that was used in the put method.
So what's going on there? Has anyone seen this problem before? I'm using j2sdk1.4.1.
Alex
 
Alex Ioannou
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, to be safe on .equals I did .equalsIgnoreCase() and used .trim() on both strings.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic