aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Custom keys in tree map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Custom keys in tree map" Watch "Custom keys in tree map" New topic
Author

Custom keys in tree map

Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
I have a doubt in custom keys in tree map. Below is the egample code which i have

Employee is the class which i try to use as key.Also,if two employees have same name, then both object should be considered equal.Therefore i have implemented hashcode and equals method as well



Now i have another class


i get output as "can i get"+null
If i had used hashmap instead of tree map, then it would have worked. i.e(with hash map i would have got value but now with treemap i get null).




Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Jack Tol
Greenhorn

Joined: Oct 27, 2009
Posts: 24

The reason why returns null with a TreeMap is because you use a comparator then.

As soon as you use a comparator, the comparator is used for equality checks. Because the comparator compares based on the number of years worked, it finds no matches. Try printing some output in the equals and in the compare method. You'll see that the equals method is never invoked, while the compare method is.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Thanks Jack.

But then in my case how do i make it work? In my compare method i have logic which sorts employees based on number of years worked.
So given the above scenario, is their a way i can make it work? (i.e getting value when i do treeMapContaingEmployees.get(e6) )
Jack Tol
Greenhorn

Joined: Oct 27, 2009
Posts: 24

Maan Suraj wrote:Thanks Jack.

But then in my case how do i make it work? In my compare method i have logic which sorts employees based on number of years worked.
So given the above scenario, is their a way i can make it work? (i.e getting value when i do treeMapContaingEmployees.get(e6) )


You could add a condition at the start of your compare method to check the name for equality (using for example o1.equals(o2)) and when equal return 0. This would mean that Employees with the same name but different number_Of_Years_Worked to be seen as one and the same, and thus only the last one added to be present your TreeMap.

Based on your code where you try to retrieve an Employee based on the name attribute, this would be correct behavior for your Map.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Thanks jack
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Maan Suraj wrote:


I don't know whether you did this on purpose as an experiment, but I want to point out two things about this iteration code.
First is that although you have created an iterator, and are using its hasNext() method as the termination condition,
you never advance through the iterator inside your loop, and so hasNext() is always true and you get an infinite loop.
Second, inside the loop you are ignoring your iterator and going back to the map to get the element e6.
The point of an iterator is to serve you the elements in your collection one at a time, so this kind of defeats its purpose.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Thanks Dennis for the suggestion.
Actually there was a lot of code in my file.I removed lots of code before posting the question here,so that it would be easier for people to read.

While doing so,i guess i removed the code which was needed.So actually the code was like this



Anyway thanks for pointing it out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Custom keys in tree map