aspose file tools*
The moose likes Beginning Java and the fly likes Comparators generic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparators generic" Watch "Comparators generic" New topic
Author

Comparators generic

Botond Lakatos
Greenhorn

Joined: Nov 25, 2011
Posts: 9

Hello
I am just getting familiar with generics. In a previous exercise we had to create a person database where we used non generic comparators, created an abstract comparator class and subclasses implemented the various compare() methods (not a part of the Collection Framework at this point)
Now I need to refactor my code to make it generic and use the Collections of Java. Having a hard time with the comparators and a bit confused. My approach was to have my abstract comparator class implement the Comparator interface. Do the subclasses also have to include this in the class declaration statement. I tried everything but cannot make it work? My goal is to be able to use the Collections.sort(list,comparator);
/ sort(List <T> list, Comparator <? super T> c) method but cannot get it work.

The declaration of my Abstract comparator class looks like this:



one of its extended classes:



How do I change the declaration of these classes to make them generic so that the Collections.sort(list,comparator) will accept the second parameter?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

In the future, when posting code, please UseCodeTags.(⇐click) I've added them to your post.

What exact error are you getting, and what is the code that is causing it?


Also, look closely at this:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
I would have thought you are better off throwing an Exception if either argument is null.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8176
    
  23

Botond Lakatos wrote:My approach was to have my abstract comparator class implement the Comparator interface.

Seems reasonable; although I'm not sure that you really have any need for your "abstract" Comparator class.

Do the subclasses also have to include this in the class declaration statement.

Not if they extend your abstract Comparator.

How do I change the declaration of these classes to make them generic so that the Collections.sort(list,comparator) will accept the second parameter?

Other than the bit that Jeff pointed out, I can't see any. As he said, show us the error you're getting, along with the piece of code that actually uses them.

Tip: It's usually a good idea to add "@Override" annotations to any method that's being overriden, viz:
@Override
public int compare(Person p1, Person p2) { ...

Specifically, it often helps to highlight any silly errors due to mis-spelling, etc.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparators generic