• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Problem with hash map

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am maintaing a map of key, value pairs. The key is a POJO and the value is a list

So it goes Map - HashMap<POJO, LIst>

While browsing an array,

some elements of the array describe the pojo and the rest describe the object that go in the list
So,

new POJO(). setProp1(arr[0]);
new POJO(). setProp2(arr[1]);
new POJO(). setProp2(arr[2]);....

new Object.setProp1(arr[3]);
new Object.setProp1(arr[4]);
new Object.setProp1(arr[5]);....

Now in case i find a pojo which is similar to the POJO in the hashmap, the corresponding object that gets created goes into the list of the key.

The problem i'm facing is when i call

Map<TaskPOJO, List<TimeEntryView>> excelResultSet = new HashMap<TaskPOJO, List<TimeEntryView>>();
if (excelResultSet.containsKey(somePOJOObject)) {

}

the condition always evaluates to fall inspite of me having overriden the POJO's .equals method.
As a result semantically similar POJOs are treated as new keys.


PLease Help asap.


 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please tell the relation between the POJO and 'array' you mentioned?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you change HashMap to TreeMap and try using a compareTo() method like below?

 
Shridhar Raghavan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

\



Here we go!!!

The esssential bit are something this way. the method receives an array list. Each object in the list is an array. So a multi dimensional array if you will. Now each row in this multidimensional defines certain attributes. Requirement is to group all those guys with the same column values together in a key value sort of relationship. My key shall be a pojo over those grouping column values. The value is a list of all those objects that share the same column values.


Thanks. I will try it out the tree thing meanwhile.
 
Shridhar Raghavan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The tree map doesnt work?
 
Shridhar Raghavan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any help?
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shridhar Raghavan wrote:the condition always evaluates to fall inspite of me having overriden the POJO's .equals method.


You need to override the hashCode method as well.
 
Shridhar Raghavan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops i goofed up with the comparable equalsTo. == and .equals error. It works now.

But could you explain the difference. The HashMap containsKey() api says the .equals method is invoked to determine if the key is contained. But the overriden .equals of my key object was never called? What am i missing?
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
AFAIK being a HashMap first it checks if HashCodes are same for your keys. If they are same then only it will go and call equals() to see if Objects are equal.

Regards,
Amit
 
Shridhar Raghavan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Forgot the basics. Thanks
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joanne's right... Corrected to implement the hashCode() method.

 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic