Yahoo India Web Search

Search results

  1. Comparable allows you to specify how objects that you are implementing get compared. Obviously, if you don't have control over a class (or you want to provide multiple ways to compare objects that you do have control over) then use Comparator. Otherwise you can use Comparable. answered Nov 5, 2010 at 17:52.

  2. Feb 15, 2010 · Use Comparable: if the object is in your control. if the comparing behaviour is the main comparing behaviour. Use Comparator : if the object is outside your control and you cannot make them implement Comparable. when you want comparing behaviour different from the default (which is specified by Comparable) behaviour.

  3. Jan 7, 2009 · compareTo() is from the Comparable interface. compare() is from the Comparator interface. Both methods do the same thing, but each interface is used in a slightly different context. The Comparable interface is used to impose a natural ordering on the objects of the implementing class.

  4. Mar 29, 2018 · Comparable and Comparator are generic interfaces that allow to compare instances of the type defined in the generic (subclasses included). The main difference between them is that Comparable is directly implemented in the class which you want to compare objects. Consequently, if you have a single way to compare instances from a class, that is ...

  5. Sep 17, 2009 · A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances. Comparator. A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances.

  6. Mar 12, 2020 · If Comparable is implemented by a class Employee, when 5000 Employee objects are created and added in an ArrayList, there will be 5000 objects with compareTo methods in heap memory. So unless absolutely necessary, don't use Comparable. With Comparator, the above mentioned memory overhead is eliminated.

  7. Jun 23, 2015 · Comparator class: public class MyComparatorOne implements Comparator<A> { public int compare(A obj1, A obj2) { return obj1.getVal().compareTo(obj2.getVal()); } } and the output is: List with 1000000 elements and 10000 runs external Comparator class average millis: 3 inline Comparator class average millis: 3

  8. Jan 21, 2016 · (See the Comparable interface or Comparator interface for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a sorted set performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the sorted set, equal.

  9. Comparator<Person> comparator = Comparator.comparing(person -> person.name); comparator = comparator.thenComparing(Comparator.comparing(person -> person.age)); Check out the link above for a neater way and an explanation about how Java's type inference makes it a bit more clunky to define compared to LINQ.

  10. May 7, 2022 · 4. There's a substantial distinction between the use cases for Comparator and Comparable. Implementing the Comparable interface is suitable for objects that have a natural order in your domain model. I'm not sure whether animals have a natural order, but if it is the case from the perspective of how your application model the animals, that's ...