Java 中开箱即用的直接比较器

发布于 2024-09-04 15:32:47 字数 619 浏览 8 评论 0原文

我有一个方法,其参数之一需要一个 Comparator 。我想传递一个进行正常比较的Comparator和一个进行相反操作的反向比较器。

java.util.Collections提供了一个reverseOrder(),这对于反向比较很有用,但我找不到任何正常的Comparator

我想到的唯一解决方案是Collections.reverseOrder(Collections.reverseOrder())。但我不喜欢它,因为里面调用了 double 方法。

当然,我可以像这样编写一个 NormalComparator

public class NormalComparator<T extends Comparable> implements Comparator<T> {
    public int compare(T o1, T o2) {
        return o1.compareTo(o2);
    }
}

但我真的很惊讶 Java 没有现成的解决方案。

I have a method which needs a Comparator for one of its parameters. I would like to pass a Comparator which does a normal comparison and a reverse comparator which does in reverse.

java.util.Collections provides a reverseOrder() this is good for the reverse comparison, but I could not find any normal Comparator.

The only solution what came into my mind is Collections.reverseOrder(Collections.reverseOrder()). but I don't like it because the double method calling inside.

Of course I could write a NormalComparator like this:

public class NormalComparator<T extends Comparable> implements Comparator<T> {
    public int compare(T o1, T o2) {
        return o1.compareTo(o2);
    }
}

But I'm really surprised that Java doesn't have a solution for this out of the box.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心凉怎暖 2024-09-11 15:32:47

大多数可以指定 Comparator 的地方也有一个根本不使用 Comparator 的版本,在这种情况下它使用自然顺序(即它期望所有对象实现 >可比较并使用compareTo)。

因此,通常的解决方案是根本不指定Comparator。您是否有支持Comparator方法的特定情况?

如果您绝对需要,Google 收藏集(以及 Guava,它是 Google Collections 的超集)提供Ordering.natural()返回Ordering 对象,表示由 Comparable 接口定义的自然顺序。 Ordering 实现了 Comparator,因此您可以简单地使用它。

Most places where you can specify a Comparator also have a version without using a Comparator at all in which case it uses the natural order (i.e. it expects all objects to implement Comparable and uses compareTo).

So the usualy solution to this is to not specify a Comparator at all. Do you have a specific case where only the Comparator approach is supported?

If you absolutely need it, the Google Collections (as well as Guava, which is a superset of the Google Collections) provides Ordering.natural() which returns a Ordering object that represent the natural order as defined by the Comparable interface. Ordering implements Comparator, so you can simply use that.

酷炫老祖宗 2024-09-11 15:32:47

但令我感到非常惊讶的是,Java 没有针对此问题提供开箱即用的解决方案。

我想它在某些情况下会很有用......就像你的情况一样。但在大多数用例中,应用程序只需直接使用对象的compareTo方法即可。通过 Comparator 对象间接进行是没有任何作用的……大多数时候。

我的猜测是,这些 Java API 的设计者并没有认为您的用例重要到足以直接支持。此外,您的实现只有四行代码。

Java 类库并不完美。学会忍受它:-)。

But I'm really surprised that Java doesn't have a solution for this out of the box.

I suppose it would be useful in a few cases ... like yours. But in most use-cases an application would simply use the object's compareTo method directly. Indirecting via an Comparator object would serve no purpose ... most of the time.

My guess is that the designers of those Java APIs did not consider your use-case important enough to support directly. Besides, your implementation is only four lines of code.

The Java class libraries are not perfect. Learn to live with it :-).

伊面 2024-09-11 15:32:47

对于反向排序,请使用Collections.reverseOrder() ...

返回一个比较器,该比较器将自然顺序的相反结果强加于
实现 Comparable 接口的对象集合。

For reverse ordering use Collections.reverseOrder() ...

Returns a comparator that imposes the reverse of the natural ordering on a
collection of objects that implement the Comparable interface.

浮萍、无处依 2024-09-11 15:32:47

通常不需要自然顺序 Comparator,因为通常有一个需要 Comparable的重载。。您始终可以按照 Collections.reverseOrder() 设置的示例编写如下内容:

private static final Comparator<?> NATURAL_ORDER =
   new Comparator<Comparable<Object>>() {
     @Override public int compare(Comparable<Object> o1, Comparable<Object> o2) {
        return o1.compareTo(o2);
     }
   };

@SuppressWarnings("unchecked")
public static <T> Comparator<T> naturalOrder() {
    return (Comparator<T>) NATURAL_ORDER;
}

然后您可以编写如下内容:

List<String> names = Arrays.asList("Bob", "Alice", "Carol");
Collections.sort(names, naturalOrder());
System.out.println(names);
// prints "[Alice, Bob, Carol]"

There is usually no need for a natural order Comparator<T>, since usually there's an overload that takes a Comparable<T>. You can always follow the example set by Collections.reverseOrder() and write something like this:

private static final Comparator<?> NATURAL_ORDER =
   new Comparator<Comparable<Object>>() {
     @Override public int compare(Comparable<Object> o1, Comparable<Object> o2) {
        return o1.compareTo(o2);
     }
   };

@SuppressWarnings("unchecked")
public static <T> Comparator<T> naturalOrder() {
    return (Comparator<T>) NATURAL_ORDER;
}

You can then write something like:

List<String> names = Arrays.asList("Bob", "Alice", "Carol");
Collections.sort(names, naturalOrder());
System.out.println(names);
// prints "[Alice, Bob, Carol]"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文