[英]Java 8: implementing Comparable
I like the new static factory methods of Comparator
, as they allow to implement Comparators in a very concise and less error-prone way. 我喜欢的新的静态工厂方法
Comparator
,因为它们允许实现非常简洁,不易出错的方式比较。
But what is the recommended way to implement Comparable
? 但是实施
Comparable
的推荐方法是什么? Should we use Comparators inside the Comparable implementation? 我们应该在Comparable实现中使用Comparators吗?
public MyClass implements Comparable<MyClass>{
...
public int compareTo(MyClass other){
Comparator<MyClass> naturalOrderComparator =
Comparator.comparing(MyClass::getFoo)
.thenComparing(MyClass::getBar);
return naturalOrderComparator.compare(this, other);
}
}
or even use a static comparator to reduce a lot of object creation when sorting huge collections: 甚至在对大型集合进行排序时使用静态比较器来减少大量对象的创建:
public MyClass implements Comparable<MyClass>{
private static final Comparator<MyClass> NATURAL_ORDER_COMPARATOR =
Comparator.comparing(MyClass::getFoo)
.thenComparing(MyClass::getBar);
...
public int compareTo(MyClass other){
return NATURAL_ORDER_COMPARATOR.compare(this, other);
}
}
Or is there another recommended way to implement Comparable with Java SE 8? 或者是否有另一种推荐的方法来实现Comparable与Java SE 8?
Your own Option 2 is almost certainly the best currently available way. 您自己的选项2几乎肯定是目前最好的方式。 It avoids allocation, it reads pretty well -- especially if you put the static constant next to the
compareTo
method. 它避免了分配,它读得很好 - 特别是如果你把静态常量放在
compareTo
方法旁边。
The new Comparator.comparing
factory methods in Java 8 are very easy to read, and even better, difficult to screw up -- there are many, many ways to write comparisons incorrectly by hand, more than I care to remember, and the factory methods are immune to most of them. Java 8中新的
Comparator.comparing
工厂方法非常容易阅读,甚至更好,很难搞砸 - 有很多很多方法可以手动编写比较错误,比我记忆中的更多,以及工厂方法对大多数人免疫。 Even though it's a little weird to use them to write a compareTo
method instead of a Comparator
object, it's still better than the alternatives. 尽管使用它们来编写
compareTo
方法而不是Comparator
对象compareTo
,但它仍然比替代方案更好。
Pre-Java-8 the general best practice was to use Guava 's ComparisonChain
and Ordering
utilities. Pre-Java-8的一般最佳实践是使用Guava的
ComparisonChain
和Ordering
实用程序。 They abstract away the cumbersome and easy-to-get-wrong details of properly implementing a .compareTo()
/ .compare()
method, and allow you to compose a human-readable sequence of steps to define how objects should be compared. 它们抽象出了正确实现
.compareTo()
/。 .compare()
方法的繁琐且容易出错的细节,并允许您编写一个人类可读的步骤序列来定义应如何比较对象。 Ordering
implements Comparator
, but there'd be nothing wrong with defining an Ordering
and invoking it in a Comparable
's .compareTo()
method. Ordering
实现Comparator
,但定义Ordering
并在Comparable
的.compareTo()
方法中调用它没有任何问题。
Note that Ordering
is described as obsolete thanks to the additions to the JDK in Java 8: 请注意,由于在Java 8中添加了JDK,
Ordering
被描述为已过时:
If you are using Java 8, this class is now obsolete.... Most of its functionality is now provided by
Stream
and byComparator
itself, and the rest can now be found as static methods in our newComparators
class.如果你使用的是Java 8,那么这个类现在已经过时了....它的大部分功能现在由
Stream
和Comparator
本身提供,其余的现在可以在我们新的Comparators
类中找到静态方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.