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. 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.

  4. Sep 13, 2018 · From the reference docs of Comparable.compareTo (T): Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. The implementor must ensure sgn (x.compareTo (y)) == -sgn (y.compareTo (x)) for all x and y.

  5. 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.

  6. The Comparator interface is used to establish an ordering for the elements being compared. Having fields that use double is irrelevant to this ordering. Your code is fine.

  7. If you are using Java 8 then it's better to use below code like this: Comparator<People> comparator = Comparator.comparing(People::getName); And then simply use: Collections.sort(list, comparator); If you are using Java 7 or below then you can use a comparator for customized sorting order by implementing compare method.

  8. Oct 7, 2015 · I have an ArrayList and want sort it in descending order. I use for it java.util.stream.Stream.sorted(Comparator) method. Here is a description according Java API: Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator. this methods return me a sort with ascending order.

  9. The java.util.Collections class has a sort method that takes a list and a custom Comparator. You can define your own Comparator to sort your Person object however you like. You can define your own Comparator to sort your Person object however you like.

  10. Jan 14, 2013 · Collection<Comparable> items; Collections.sort(items); If items are not Comparable, they need a Comparator object to do the comparison: Collections<T> items; Collections.sort(items, comparator); A bridging Comparator is trivial, and you did it already. Wrapping every T item with some Comparable adapter having a Comparator, seems useless. First ...

  1. People also search for