File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes TreeMap equals check Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "TreeMap equals check" Watch "TreeMap equals check" New topic
Author

TreeMap equals check

Anirban Chowdhury
Ranch Hand

Joined: Aug 05, 2008
Posts: 36
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.

To living life on the edge! I blog my experiences @ http://anirbanchowdhury.wordpress.com.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Aug 05, 2008
Posts: 36
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Aug 05, 2008
Posts: 36
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.
 
 
subject: TreeMap equals check