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


Rick Charles

Joined: Jul 27, 2006
Posts: 25
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
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The best solution by far would be to use "select distinct" in the original query -- can't you just do that?

[Jess in Action][AskingGoodQuestions]
Rick Charles

Joined: Jul 27, 2006
Posts: 25

I have no control over modifying the query.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
equals method would work better than a Comparator.
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
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.
I agree. Here's the link:
subject: Comparators
It's not a secret anymore!