Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Comparator of Comparable<? extends Object>

Thread view: 
SD - 16 May 2006 06:26 GMT
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;
  }
}
Oliver Wong - 16 May 2006 23:04 GMT
>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
[quoted text clipped - 35 lines]
>         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)
>   {
[quoted text clipped - 3 lines]
>         return false;
>   }

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

> }

   - Oliver
Roedy Green - 22 May 2006 06:32 GMT
On Tue, 16 May 2006 05:26:30 GMT, SD
<thow_away_email_111@personal.fishh2o.com> wrote, quoted or indirectly
quoted someone who said :

>compareTo(capture of ? extends java.lang.Object) in

that's not how you do it.  You want to narrow the possibilities for
the parms. That's why you are using generics.

See http://mindprod.com/jgloss/comparable.html
http://mindprod.com/jgloss/comparator.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.