Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From <a href="http://web.archive.org/web/20090126135350/http://leepoint.net/notes-java/data/expressions/22compareobjects.html" rel="noreferrer">JavaNotes</a>:</p> <ul> <li><p><code>a.compareTo(b)</code>:<br> <strong>Comparable interface :</strong> Compares values and returns an int which tells if the values compare less than, equal, or greater than.<br> If your class objects have a <strong>natural order</strong>, implement the <code>Comparable&lt;T&gt;</code> interface and define this method. All Java classes that have a natural ordering implement <code>Comparable&lt;T&gt;</code> - Example: <code>String</code>, <a href="http://www.javatpoint.com/wrapper-class-in-java" rel="noreferrer">wrapper classes</a>, <code>BigInteger</code></p></li> <li><p><code>compare(a, b)</code>:<br> <strong>Comparator interface :</strong> Compares values of two objects. This is implemented as part of the <code>Comparator&lt;T&gt;</code> interface, and the <strong>typical use is to define one or more small utility classes that implement this, to pass to methods such as <code>sort()</code> or for use by sorting data structures such as <code>TreeMap</code> and <code>TreeSet</code></strong>. You might want to create a Comparator object for the following:</p> <ul> <li><strong>Multiple comparisons</strong>. To provide several different ways to sort something. For example, you might want to sort a Person class by name, ID, age, height, ... You would define a Comparator for each of these to pass to the <code>sort()</code> method.</li> <li><strong>System class</strong> To provide comparison methods for classes that you have no control over. For example, you could define a Comparator for Strings that compared them by length.</li> <li><strong>Strategy pattern</strong> To implement a Strategy pattern, which is a situation where you want to represent an algorithm as an object that you can pass as a parameter, save in a data structure, etc.</li> </ul></li> </ul> <p>If your class objects have one natural sorting order, you may not need compare(). </p> <hr> <p>Summary from <a href="http://www.digizol.com/2008/07/java-sorting-comparator-vs-comparable.html" rel="noreferrer">http://www.digizol.com/2008/07/java-sorting-comparator-vs-comparable.html</a></p> <p><strong>Comparable</strong><br> A comparable object is capable of comparing itself with another object.</p> <p><strong>Comparator</strong><br> A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. </p> <hr> <p>Use case contexts:</p> <p><strong>Comparable interface</strong></p> <p>The equals method and <code>==</code> and <code>!=</code> <strong>operators</strong> test for equality/inequality, but <strong>do not provide a way to test for relative values</strong>.<br> Some classes (eg, String and other classes with a natural ordering) implement the <code>Comparable&lt;T&gt;</code> interface, which defines a <code>compareTo()</code> method.<br> You will want to implement <code>Comparable&lt;T&gt;</code> in your class if you want to use it with <code>Collections.sort()</code> or <code>Arrays.sort()</code> methods.</p> <p><strong>Defining a Comparator object</strong></p> <p>You can create Comparators to <strong>sort any arbitrary way for any class</strong>.<br> For example, the <code>String</code> class defines the <a href="http://www.java2s.com/Code/JavaAPI/java.lang/StringCASEINSENSITIVEORDER.htm" rel="noreferrer"><code>CASE_INSENSITIVE_ORDER</code> comparator</a>. </p> <hr> <p>The difference between the two approaches can be linked to the notion of:<br> <strong>Ordered Collection</strong>: </p> <p>When a Collection is ordered, it means you can iterate in the collection in a specific (not-random) order (a <code>Hashtable</code> is not ordered).</p> <p>A Collection with a <strong>natural order</strong> is not just ordered, but <strong>sorted</strong>. Defining a natural order <a href="http://www.codinghorror.com/blog/archives/001018.html" rel="noreferrer">can be difficult!</a> (as in <a href="http://web.archive.org/web/20090310030619/http://weblogs.java.net/blog/skelvin/archive/2006/01/natural_string.html" rel="noreferrer">natural String order</a>).</p> <hr> <p>Another difference, pointed out by <a href="https://stackoverflow.com/users/57344/haveaguess">HaveAGuess</a> in <a href="https://stackoverflow.com/questions/420223/what-is-the-difference-between-compare-and-compareto/420240?noredirect=1#comment46816131_420240">the comments</a>:</p> <ul> <li><code>Comparable</code> is in the implementation and not visible from the interface, so when you sort you don't really know what is going to happen. </li> <li><code>Comparator</code> gives you reassurance that the ordering will be well defined. </li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload