Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Comparators generic

 
Greenhorn
Posts: 9
Eclipse IDE Firefox Browser Chrome
  • 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?
 
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:

 
Marshal
Posts: 70370
283
  • 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.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic