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

Comparators generic

 
Botond Lakatos
Greenhorn
Posts: 9
Chrome Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought you are better off throwing an Exception if either argument is null.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic