Comparator of Comparable<? extends Object>

S

SD

I can't figure out how to do this. I'm converting my existing code to
use generics but I'm stuck on this Comparator. Any ideas of how to get
this to work.

The line:
return comparable1.compareTo(comparable2);
is the one giving me problems (javac output below). How do I get the "?
extends Object" part of comparable2?

Thanx. SD

-------------------

NotificationManagerComparator.java:29:
compareTo(capture of ? extends java.lang.Object) in
java.lang.Comparable<capture of ? extends java.lang.Object>
cannot be applied to
(java.lang.Comparable<capture of ? extends java.lang.Object>)
return comparable1.compareTo(comparable2);
^


-------------------
class NotificationManagerComparator
implements java.util.Comparator<Comparable<? extends Object>>
{
public int compare(
Comparable<? extends Object> comparable1,
Comparable<? extends Object> comparable2)
{
final Class class1 = comparable1.getClass();
final Class class2 = comparable2.getClass();

if(class1 == class2)
{
return comparable1.compareTo(comparable2);
}
else
return class1.getName().compareTo(class2.getName());
}

public boolean equals(Object obj)
{
if(getClass() == obj.getClass())
return this == obj;
else
return false;
}
}
 
O

Oliver Wong

SD said:
I can't figure out how to do this. I'm converting my existing code to
use generics but I'm stuck on this Comparator. Any ideas of how to get
this to work.

The line:
return comparable1.compareTo(comparable2);
is the one giving me problems (javac output below). How do I get the "?
extends Object" part of comparable2?

Thanx. SD

-------------------

NotificationManagerComparator.java:29:
compareTo(capture of ? extends java.lang.Object) in
java.lang.Comparable<capture of ? extends java.lang.Object>
cannot be applied to
(java.lang.Comparable<capture of ? extends java.lang.Object>)
return comparable1.compareTo(comparable2);
^


-------------------
class NotificationManagerComparator
implements java.util.Comparator<Comparable<? extends Object>>
{
public int compare(
Comparable<? extends Object> comparable1,
Comparable<? extends Object> comparable2)
{
final Class class1 = comparable1.getClass();
final Class class2 = comparable2.getClass();

if(class1 == class2)
{
return comparable1.compareTo(comparable2);
}
else
return class1.getName().compareTo(class2.getName());
}

Try changing your signature to:

public <T> int compare(Comparable<T> comparable1, Comparable<T> comparable2)
{


public boolean equals(Object obj)
{
if(getClass() == obj.getClass())
return this == obj;
else
return false;
}

This code essentially does the same thing as Object.equals' method, so
there's no point on overriding it.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top