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.
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.
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?
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.