• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java HashMap returning wtong values for the given key.

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a HashMap the following HashMap



I am trying to add a new entry in HashMap against an Integer key and if the key exists then I want to fetch the corresponding list and add new item in it. Code is given below.


The real problem lies in the part where a key already exists and I try to use HashMap get function



It is not giving null is if couldn't find the value for Integer key but it gives wrong ArrayList quite often. Now I know HashMap works with equals and hashCode so I tried to Override these functions and used them instead of Integery Key.



and changed my map to use this new class for key instead of Integer, but I am still stuck on the same problem where HashMap is giving wrong value. A bit detailed snipped is available at this link https://gist.github.com/MAnfal/dc581234ce597a8ff062
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please give a listing for your CustomClass class.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can you provide a SSCCE that demonstrates your issue?

Henry
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As-salamu alaykum, Muhammad. Welcome to the Ranch.

The code at your link is pretty long. We can do our best job of helping you if you can reduce your example to a SSCCE (Small Self-Contained Compilable Example), one that shows the behavior you are describing, and post that here.
 
Muhammad Anfal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wa Aalikum Assalam, the shortest code is attached in the post itself, I thought maybe longer one will be required for more detail but the post has sufficient info. I am also online so I will looking out for any question you might ask.
 
Muhammad Anfal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Les Morgan wrote:Please give a listing for your CustomClass class.



You can view my CustomClass in the gist I attached it is named under DepartmentItemHolder
 
Muhammad Anfal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Can you provide a SSCCE that demonstrates your issue?

Henry



I am afraid the code is very long and I have tried my very best to condense it in the post, as for the example I am unable to recreate this issue in small self contained problem but overall the issue is that I not getting the right ArrayList for the Integer key I provide to the HashMap
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, your CustomHashMapIntegerKey class is just a wrapper for Integer, except for the defect that it's a mutable class. So if you call the setKey() method of a CustomHashMapIntegerKey object then that object's hash code changes. And that's a Very Bad Thing for an object which is to be used as the key of a Map.

So don't use that. Go back to using Integer as your key, since Integer should work perfectly well as a key and your CustomHashMapIntegerKey has at least one flaw and possibly others.

And then as for your problem, it looks like you're going to have to come up with your own SSCCE since we don't know anything except that redacted code fragment you posted. For all we know the code is being run in multiple threads and there are race conditions happening, but you didn't say anything about where the code is running either.
 
Muhammad Anfal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Well, your CustomHashMapIntegerKey class is just a wrapper for Integer, except for the defect that it's a mutable class. So if you call the setKey() method of a CustomHashMapIntegerKey object then that object's hash code changes. And that's a Very Bad Thing for an object which is to be used as the key of a Map.

So don't use that. Go back to using Integer as your key, since Integer should work perfectly well as a key and your CustomHashMapIntegerKey has at least one flaw and possibly others.

And then as for your problem, it looks like you're going to have to come up with your own SSCCE since we don't know anything except that redacted code fragment you posted. For all we know the code is being run in multiple threads and there are race conditions happening, but you didn't say anything about where the code is running either.



I was using Integer before, I am using Integer now, I made a custom class just to check if hashCode() and equals() are being called by HashMap plus this code is running in an android app and is only accessible to main UI thread with a single listener to this call, so rest assured race conditions cannot occur in this scenario. As for SSCCE I am trying to replicate this problem and will upload it ASAP.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good luck with your SSCCE then, and do let us know what you find (even if you find something horribly obvious and fix it yourself). I for one am curious about what's going on there.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Paul specified, you could go back to using an Integer. Here is a short code that I wrote to test your hypothesis and It works fine:




output:


As you can see, it adds multiple items to the map with key as 2.
 
Muhammad Anfal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote something similar to that as well just to test and it is working well, but when I run same code in the app it start producing that bug. I dunno how and I am quite curious to find it out.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Muhammad Anfal wrote:I wrote something similar to that as well just to test and it is working well, but when I run same code in the app it start producing that bug. I dunno how and I am quite curious to find it out.


Normally, I too would insist on a SSCCE here, but lets look at the issue from a different point of view.. can you add sysouts/loggers to your code where you add the key and then share the relevant loggers with us ?

For example:


output:
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A sound advice, although I doubt if the problem is in this part.

I just had a look at the 'DepartmentItemHolder' class, and the
DepartmentItemDataObject, and I see that there is a method that
sets the 'key' of such a 'DepartmentItemDataObject'.

So it is possible that you put a DepartmentItemHolder object, with a
given key, into your Map, then later change that key, and, again later,
you use that key to retrieve the List. But since the key has changed it
will in all likelyhood be another List. Well, maybe you can have a look if
that is happening somewhere.
 
Muhammad Anfal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just had a thought, assume if I were to supply 2 different person objects with different ids simultaneously to this line of code from salvin francis the gentleman above



I will end up entering them in the same list instead of their separate lists? assuming both the code and supplier of person objects are working on the main ui thread.
 
Muhammad Anfal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:A sound advice, although I doubt if the problem is in this part.

I just had a look at the 'DepartmentItemHolder' class, and the
DepartmentItemDataObject, and I see that there is a method that
sets the 'key' of such a 'DepartmentItemDataObject'.

So it is possible that you put a DepartmentItemHolder object, with a
given key, into your Map, then later change that key, and, again later,
you use that key to retrieve the List. But since the key has changed it
will in all likelyhood be another List. Well, maybe you can have a look if
that is happening somewhere.



That could be a possibility in the scenario where I am using the key at all, but unfortunately it was there just for testing as you can see in CustomHashMapKey I am calling hashCode and equals and I wanted to confirm both are getting called by the HashMap.
 
Muhammad Anfal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on my assumption I went ahead and first added all the holders into a list and then when list had about 50 items I inserted them in the HashMap and printed the result and it worked, somehow even though both the caller and called function were on same thread (Main UI thread) and the function was enforcing that new List Item should be added to proper key only this bug was occuring, it was due to a race condition and it is as far as I know but it would've been more appropriate if HashMap totally skipped some values but why would it enter wrong List Item to wrong Id I still don't know but atleast I have the solution now. And thanks for the help everyone, I was able to make this assumption based on the scenarios you provided.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Muhammad Anfal wrote:I just had a thought, assume if I were to supply 2 different person objects with different ids simultaneously to this line of code
I will end up entering them in the same list instead of their separate lists? assuming both the code and supplier of person objects are working on the main ui thread.


No you won't. If the ID is different, you will add it to a different List.

Which begs the question: Where does your Integer "ID" come from, and what does it mean?

However, even without that, an alternative to what you've been doing is as follows:which saves a fair bit of faffing around.

Winston
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Muhammad Anfal wrote:I made a custom class just to check if hashCode() and equals() are being called by HashMap.



Just for future reference: It does occasionally happen that code in the standard Java API contains bugs. But when you make the hypothesis that an API bug is the source of your code's problems, the chances are very high that you're wasting your time. Especially when you're talking about code which has been in the language since the beginning of time. So when you're chasing down a bug, you should always assume that it's your fault.
 
Muhammad Anfal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Muhammad Anfal wrote:I made a custom class just to check if hashCode() and equals() are being called by HashMap.



Just for future reference: It does occasionally happen that code in the standard Java API contains bugs. But when you make the hypothesis that an API bug is the source of your code's problems, the chances are very high that you're wasting your time. Especially when you're talking about code which has been in the language since the beginning of time. So when you're chasing down a bug, you should always assume that it's your fault.



Thanks for the advice will keep it in mind.
 
Muhammad Anfal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Muhammad Anfal wrote:I just had a thought, assume if I were to supply 2 different person objects with different ids simultaneously to this line of code
I will end up entering them in the same list instead of their separate lists? assuming both the code and supplier of person objects are working on the main ui thread.


No you won't. If the ID is different, you will add it to a different List.

Which begs the question: Where does your Integer "ID" come from, and what does it mean?

However, even without that, an alternative to what you've been doing is as follows:which saves a fair bit of faffing around.

Winston



My thoughts exactly are the same just wanted to know if there can be some way it may occur (which I am sure can't, but you can't be too sure about anything :p). I did find the bug at the end it was due to race condition but at the end what I couldn't understand was if 2 or more IDs were supplied at the same time and hashmap was getting the list based that ID how in the world would it add foreign IDs to the list, skipping some of the IDs makes more sense to me.
 
Popeye has his spinach. I have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic