• Post Reply Bookmark Topic Watch Topic
  • New Topic

TreeMap not giving complete output  RSS feed

 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a student profile with roll number, standard, firstname and last name.

I have designed it in TreeMap. And it is not giving the complete output . What is the issue.




Student class




Output :

roll no=1 standard=5 firstname=Anagha lastname=Prasad  2
roll no=2 standard=4 firstname=Swapnil lastname=Patil  6
roll no=3 standard=2 firstname=Mahesh lastname=Wange  5
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are implementing the class's natural order via Comparable and compareTo(). In your compareTo() you are telling it that if you have two objects with the same rollno that they are the same and TreeMap only permits one at a time and the last one entered wins.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your compareTo() method isn't consistent with your equals() method. One says that two Students are equal if their rollno's are equal (which is why there are only three students in the tree because there are only three different rollno's) and the other says that two Students are equal if their first and last names are the same.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:... and the other says that two Students are equal if their first and last names are the same.

True for equals() but the implementation of hashCode() is not consistent with equals().
 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Paul and Carey . then what should write in compareTo and hashcode() and equals to make the program consistent
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your hashCode() is based on all four fields whereas your equals() only uses firstname and lastname.

In equals():
What if Object o is null?
What if Object o is not a Student?
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way to make the compareTo() method consistent-with-equals is by basing the equals() method implementation on compareTo():

I question the decision to override compareTo(), equals() and hashCode() on entity types though. For instance, if a student changes their name, are they suddenly a different student?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!