KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > lang > Comparable

java.lang
Interface Comparable<T>

All Known Subinterfaces:
Delayed, Name, ScheduledFuture<V>
All Known Implementing Classes:
Authenticator.RequestorType, BigDecimal, BigInteger, Boolean, Byte, ByteBuffer, Calendar, Character, CharBuffer, Charset, CollationKey, CompositeName, CompoundName, Date, Date, Double, DoubleBuffer, ElementType, Enum, File, Float, FloatBuffer, Formatter.BigDecimalLayoutForm, FormSubmitEvent.MethodType, GregorianCalendar, IntBuffer, Integer, JTable.PrintMode, KeyRep.Type, LdapName, Long, LongBuffer, MappedByteBuffer, MemoryType, ObjectStreamField, Proxy.Type, Rdn, RetentionPolicy, RoundingMode, Short, ShortBuffer, SSLEngineResult.HandshakeStatus, SSLEngineResult.Status, String, Thread.State, Time, Timestamp, TimeUnit, URI, UUID
See Also:
Top Examples, Source Code, Comparator, Collections.sort(java.util.List), Arrays.sort(Object[]), SortedSet, SortedMap, TreeSet, TreeMap

public int compareTo(Object o)
See Also:
ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[904]Old way to implement Comparable
By Anonymous on 2004/09/17 16:53:10  Rate
public class Foo implements  Comparable  {  
   String primaryKey; 
  
  
   public int compareTo ( Object o )   {  
     Foo foo =  ( Foo )  o; 
     return primaryKey.compareTo ( foo.getPrimaryKey (  )  ) ; 
    }  
  
  
    public String getPrimaryKey (  )   {  
        return primaryKey; 
     }  
     
  } 


[1296]New way to implement Comparable
By mandawah on 2005/02/08 05:21:36  Rate
I think it's better like this  ( for 5.0 only ) : 
  
  
 public class Foo implements Comparable < Foo >   {  
 String primaryKey; 
  
  
 public int compareTo ( Foo o )   {  
 return primaryKey.compareTo ( o.getPrimaryKey (  )  ) ; 
  }  
  
  
    public String getPrimaryKey (  )   {  
    return primaryKey; 
     }  
     
  } 


int compareTo(T o)
See Also:
ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1302]Implement Comparable using template
By Anonymous on 2005/05/19 13:47:08  Rate
Wow, This was so hard to find, actual code that helps 
  
  
 static  < T extends Comparable < T >  >  T max2 ( T obj1, T obj2 )   {  
 T result = obj1; 
 if  ( obj1.compareTo ( obj2 )   <  0 )  // Won't cause Java 5.0 To Blow Up 
 result = obj2; 
 return result; 
  }  
  
  
 Take Care, Wish you the best

Popular Tags