In java, what comes from a comparable average

I see code like this

class A implements Comparable<A> { } 

What does this mean, what are its advantages and disadvantages?

+4
source share
6 answers

This means that the class agrees to respond to methods defined by the " interface " Comparable .

The advantage that you have with this (and any other declaration "implements") is that you can " abstract " the type of object and code for the interface.

Consider this

 class A implements Comparable { .... } class B implements Comparable { .... } class C implements Comparable { .... } 

Then you can encode something that you can use Comparable instead of a specific type:

 public void doSomethingWith( Comparable c ) { c.compareTo( other ); // something else... } 

And call it like this:

  doSomethingWith( new A() ); doSomethingWith( new B() ); doSomethingWith( new C() ); 

Since you don't care what type of class you just need it to implement the interface.

This (a program for an interface, not an implementation) is one of the most powerful methods in the OO programming world, as it promotes low-connect .

+4
source

This means that a class is one that can be controlled by functions that expect their arguments to be objects that can be compared with other objects of the same type (such as a predefined sorting function for lists).

The implementation of the Comparable interface means that the class supports certain functions that the interface requires (in particular, the compareTo() method), that sorting operations (or others) performed in the class will be used to perform their work, not to worry about the rest of the class.

More details: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html

+1
source

This means that class A can be sorted using the Comparable compareTo method:

 A a1 = new A(1); A a2 = new A(3); // -1, 0, or 1 depending on whether a2 is less than, equal to, or greater than a1 int order = a1.compareTo(a2); 

Comparable uses the natural order for your class.

Another way to upgrade from Java 5 is Comparator . You can pass this object around and have several ways to compare and sort the target class. For example, sometimes you may need to sort the name class by name, and sometimes by name. Comparable only gives you one way to do this, but you can have multiple instances of Comparator.

+1
source

Implementing a comparable interface means that A can be compared to other instances of A.

Many operations in java that include sorting use the methods defined in the Comparable interface to determine if there are more instances of A than less than or equal to other instances.

Using these methods, you can use many convenient functions, such as sorting java, using instances of A as keys for binary trees, etc.

+1
source

In addition to what everyone else said, implementing the interface (or extending the class), you get the compiler to execute the supertype contract compiler. In the case of "Comparable", this means that you get a compiler error if you do not implement the int compareTo (A anA) method in the implementation class. Adding the @Override annotation to the implementation method provides even greater compile-time security; if you were unable to implement the method with the correct signature, the compiler will tell you. Compile-time errors are much simpler and cheaper than runtime errors. In addition, the implementation of the interface allows us to consider any instance of the implementation class as an interface type for methods (and constructors) that take an interface type as an argument or a general parameter. For example, the method "java.util.Collections.max (Collection coll)" accepts a collection whose base type should extend "Comparable".
http://download.oracle.com/javase/7/docs/api/java/util/Collections.html#max(java.util.Collection)

+1
source

This means that objects of this class can be easily sorted in collections, because they can be compared with each other. Another option is to implement a Comparator, which is the class responsible for sorting other classes. Comparable puts the sorting logic directly into the sorting class; The comparator places the sorting logic in another class.

0
source

All Articles