aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparators" Watch "Comparators" New topic
Author

Comparators

Rick Charles
Greenhorn

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
Rick!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

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
Greenhorn

Joined: Jul 27, 2006
Posts: 25
Hi

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

Joined: Apr 13, 2003
Posts: 1088
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: http://aspose.com/file-tools
 
subject: Comparators
 
Similar Threads
Getting Two Tables to Line Up
Hibernate: Restrict fields in SELECT statement
Varaible row count in JTable
Generating tables
Display contents of HTML table row