Does a natural comparator exist in the standard api?

孤街浪徒 提交于 2019-11-30 01:10:09
Julien

Added to Comparator in Java 8:

static <T extends Comparable<? super T>> Comparator<T> naturalOrder()

Use it like this, for example:

Comparator<Double> natural = Comparator.<Double>naturalOrder();
return natural.compare(1.0, 1.1));

Yes, the JDK definitely has it! Here it is:

Collections.reverseOrder(Collections.reverseOrder())

Just kidding. (But it's true. (Just don't actually use that. (Ever.)))

JDK does not have it, however it is called ComparableComparator and it exists in many frameworks such as Spring, Apache Commons, Hibernate and many others

I am not familiar with a default comparator in Java, but obviously, Comparator to compareTo is often a mere wrapper.

There is no general of "natural ordering" in the standard API, although certain built in types, like numbers, have an implementation of compareTo which then becomes their natural ordering.

TreeMap and TreeSet and all these should throw a RuntimeException if the object you put in does not implement Comparable. Thus, for example, you could throw in strings or numbers but not another collection.

The code of TreeMap does not use a comparator if one is not available - it uses compareTo instead. To use compareTo, it does a cast to Comparable, which is the source of the exceptions.

    private int compare(K k1, K k2) {
      return (comparator==null ? ((Comparable <K>)k1).compareTo(k2)
                                : comparator.compare((K)k1, (K)k2));
  }

I think if a class has a natural ordering, it is more usual in Java for it to implement Comparable rather than have a Comparator implementation for each class.

Thus, if the objects in question have a natural ordering defined, they must implement Comparable and have the compareTo method defined. No need to go looking for a Comparator. Most classes in java.util take either an optional Comparator if there is any specific ordering to be imposed, or simply try to call compareTo on the objects if there is no other ordering specified.

So, long story short: Implement Comparable whenever you want to impose a natural ordering on a class, only use a Comparator when you want something other than the natural ordering.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!