Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparators

 
Rick Charles
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guyz

I have a scenario where i get a resultset retreived from the database. I want to remove duplicate rows provided if all the columns in the row contain duplicates. Each and every row is represented by a java bean (ie) all the columns will be set to a variable using its setter methods .I know the best solution is to go for a comparator but i havent used it . Could anyone of you guys can guide me to use it and also is there any other way to compare objects.

Thanks in advance
Rick!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best solution by far would be to use "select distinct" in the original query -- can't you just do that?
 
Rick Charles
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have no control over modifying the query.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
equals method would work better than a Comparator.
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anupam is right. Create an equals method that compares every data member to define equality. Comparators are used when you need alternate definitions for equality. For example, you could have different Comparators to match objects base on an id number, a name, a description, or any combination of fields.

First implement the equals method and only use Comparators when you need to override it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic