Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Custom keys in tree map

 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).


 
Greenhorn
Posts: 24
Android Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
Android Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks jack
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!