• Post Reply Bookmark Topic Watch Topic
  • New Topic

equals() and hashCode() methods  RSS feed

 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am trying to avoid duplicates in arraylist by overriding equals() and hashCode() methods, but it is not working fine can any one check my code, and let me know what is my mistake.

and here is my client class


in this code vishnu is repeated i want to prevent this duplicates.

output getting is: [vishnu, vardhan, pinki, friends, vishnu]
what i am expecting is: [vardhan, pinki, friends, vishnu]

thanks,
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to avoid duplicates in arraylist by overriding equals() and hashCode() methods, but it is not working fine can any one check my code, and let me know what is my mistake.


The ArrayList collection doesn't use the equals() or hashCode() methods. And it doesn't do anything to prevent duplicates either. Perhaps you want the HashSet class?

Henry
 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Thanks for your valuable reply.

thanks,
 
Campbell Ritchie
Marshal
Posts: 56197
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your equals() method on Employee doesn't look right. I think you have a ! too many.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the above does not seem to be consistent as also pointed out by Campbell. I think ClassCastException should be the result if at any time that condition satisfies.

 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell / Himanshu

Thanks for your reply, I am not getting any exception,can any one tell me the mistake in equals() method.

and I added this code in my client class still it is not giving correct result as I mentioned in my first post.


thanks,
reshma
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reshma reddy wrote:I am not getting any exception,can any one tell me the mistake in equals() method.

I think Campbell already did:
Campbell Ritchie wrote:I think you have a ! too many.

In this case you have exactly one !, and in this case, that's one too many.

Ask yourself: what does that ! do? What do you want it to do? And what would happen if you simply removed it?
 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi mike,

I done few changes in my equals() method it is working fine, if i will give name as "vishnu","Vishnu" it is givining both in result i want to ignorecase in result i tried with this not working well
in compareTo()


equals() method

regards,
reshma
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Reshma,

First Set considers equality using hashcode() and equals() method. Here the hashcode is different for "Vishnu" and "vishnu". Your implementation of hashcode is very generic and does not consider ignoreCase().

Add ignorecase in equals and change the logic in hashcode so that it returns same hascode for both the Strings.
 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi balu,
thanks for your reply,

what about comparable/comparator in set it won't do anything in set ??

thanks
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Reshma,

Comparable and Comparator deals with sorting , so any collection which is sorted uses Comparable or Comparator. In Set hierarchy TreeSet is sorted and uses compareTo() method depending on implementation.

Whereas HashSet is not sorted neither ordered , hence you don't need comparable interface and its methods.
 
Harshavardhan Reddy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Treeset for the same..... instead of arraaylist
 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Thanks for replys, I got clear picture about set.

Thanks,
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!