Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeMap equals check

 
Anirban Chowdhury
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a nice problem - **create a phone book** - containing list of contacts.
As a phonebook goes,

1. Contacts are to be always sorted.(by name)
2. Can star mark certain contacts, so they have to be above all the rest.(the * contacts are ordered by the time of contact creation)



Problems faced:

1. The contains doesn't find the entry anymore, what's amiss?
I did try and put an equals and a hashcode on Contact, apparently, if there is a Comparator/Comparable present, the compare* is only invoked.
2. Is it fair to use a TreeSet here, or should any other datastructure be used?
For eg. HashSet and then convert to a TreeSet?
3. The contains() doesn't even compare for all entries in the map, it just compared against C,Ma and Ta entries. Why was that?

Questions priority according to order.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anirban Chowdhury wrote:

1. The contains doesn't find the entry anymore, what's amiss?
I did try and put an equals and a hashcode on Contact, apparently, if there is a Comparator/Comparable present, the compare* is only invoked.


//change the priority later for one of my friends.
System.out.println(phoneBook);
frnd1.starContact = true; (comment this line and test) . normally use immutable key to TreeSet (Ofcourse in Map as well) to avoid confusion:
you put a data using x tag and later you change it to y and searching, hence the tree can't find

as a side note: use hashCode/equals to hash related collection like HashMap and use comparator/comparable to tree related collection like *Tree*Map

Anirban Chowdhury wrote:
2. Is it fair to use a TreeSet here, or should any other datastructure be used?

fair enough
Anirban Chowdhury wrote:
3. The contains() doesn't even compare for all entries in the map, it just compared against C,Ma and Ta entries. Why was that?

Yup, that is because of the fast search called binary search . you can do this search in TreeSet (Red-Black tree).
 
Anirban Chowdhury
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect. Thanks a lot. I first find it out and then change the value. Just wanted to confirm one thing, IMO, the compareTo() is never going to compare itself to itself, meaning this would never be equal to otherContact. Isn't that right?
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anirban Chowdhury wrote:IMO, the compareTo() is never going to compare itself to itself, meaning this would never be equal to otherContact. Isn't that right?


No, you shouldn't rely on that. In general you should expect that compareTo(X, X) may be called, and it should always return 0.
 
Anirban Chowdhury
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for that info. Now just to be complete, lets put the compareTo() this way,



Please do suggest if you still see any shortcomings.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic