Problem with hash map
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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]
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Can you change HashMap to TreeMap and try using a compareTo() method like below?
Shridhar Raghavan
Ranch Hand
Posts: 71
posted 14 years ago
\
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
\
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
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The tree map doesnt work?
Shridhar Raghavan
Ranch Hand
Posts: 71
Shridhar Raghavan
Ranch Hand
Posts: 71
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
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?
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Got it. Forgot the basics. Thanks
John Jai
Rancher
Posts: 1776
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Joanne's right... Corrected to implement the hashCode() method.
| All of the following truths are shameless lies. But what about this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |






