• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparator as method argument  RSS feed

 
Henrique Boreg
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello..
I have a class implementing Comparator and I don't want the compare(Object o1, Object o2) method to be defined, so I could send a Comparator as an argument so it would compare objects of that class using diferrent comparators.

For example, say I hava a ContactLit class which has a list of contacts (int id and String name), and I want one comparator to compare using the id and the other one using the name, so that later on I could maybe have a customized getContact(Comparator criteria), which would receive the comparator criteria to be used to fetch the results instead of having 2 distinct method logics for getContact(String name) and getContact(int id).

Thanks.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henrique Boreg:
... I have a class implementing Comparator and I don't want the compare(Object o1, Object o2) method to be defined...

It seems that instead of a class that is-a Comparator, you want a class that has-a Comparator (or two). You could then write methods that take one of these Comparators as an argument.

However, I don't think I'm following the reasoning. This actually seems more trouble than "having 2 distinct method logics for getContact(String name) and getContact(int id)" -- and I'm not even sure how these would accomplish the same things.

 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is your question? Are you saying that ContactLit implements Comparator, but you don't want to define the comparTo() method? If this is the case, then ContactLit shouldn't implement Comparator at all. This is fine since you will have a method that takes a Comparator as a parameter.

Layne
 
Henrique Boreg
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I would actually be trying to accomplish is rewrite the default equals() method of a class with a certain comparator which is sent to a method as an argument, not specifically having this class implement Comparator. Is there any way to do this?

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, easy:



You were expecting something else?
[ September 23, 2005: Message edited by: Ernest Friedman-Hill ]
 
Henrique Boreg
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I couldn't find my way around that, now I see how it can be done, thanks Ernest Friedman-Hill
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, just as a clarification.

You actually implement the Comparable interface, or you can create a class that extends Comparator. But in loose terms when you extend Comparator you are also implementing some methods that you want to override.

Mark
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!