File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Problem with hash map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with hash map" Watch "Problem with hash map" New topic
Author

Problem with hash map

Shridhar Raghavan
Ranch Hand

Joined: May 27, 2010
Posts: 71
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.


Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
Can you please tell the relation between the POJO and 'array' you mentioned?


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Can you change HashMap to TreeMap and try using a compareTo() method like below?

Shridhar Raghavan
Ranch Hand

Joined: May 27, 2010
Posts: 71

\



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

Joined: May 27, 2010
Posts: 71
The tree map doesnt work?
Shridhar Raghavan
Ranch Hand

Joined: May 27, 2010
Posts: 71
Any help?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
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.


Joanne
Shridhar Raghavan
Ranch Hand

Joined: May 27, 2010
Posts: 71
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?
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 514
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

Joined: May 27, 2010
Posts: 71
Got it. Forgot the basics. Thanks
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Joanne's right... Corrected to implement the hashCode() method.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with hash map