简体   繁体   English

为什么数字不使用可比/比较器

[英]Why doesn't Number use Comparable/Comparator

Short question; 简短的问题;

I've read the Javadocs for Comparable and Comparator and I don't understand the differences. 我已经阅读了Comparable和Comparator的Javadocs,但我不了解它们之间的区别。 Comparable throws a NullPointerException and Comparable is implemented by many more classes, so its methods are more readily available, are they the only differences? Comparable抛出NullPointerException,而Comparable由更多的类实现,因此它的方法更容易使用,是唯一的区别吗?

The second part of my question is Why doesn't Number implement Comparable but something like BigDecimal does? 我的问题的第二部分是为什么Number不能实现Comparable,而BigDecimal这样的东西呢? It would appear that only AtomicInteger and AtomicLong do not implement Comparable from the Number class. 似乎只有AtomicInteger和AtomicLong不能从Number类实现Comparable。 Is this why? 这是为什么? Why aren't 'Atomic' classes Comparable? 为什么“原子”类不具有可比性?

Long Question; 长问题;

I ask this because I am attempting to take a JIVariant from J-Interop convert it to a Object (has a possibility to be a Boolean, String or Number) for easy displaying and performing a switch on the return, in the event that its a Number I wish to use relational operators to determine how it compares to each individual operators. 我之所以这样问,是因为我试图将JIVariant从J-Interop转换为对象(可以是布尔值,字符串或数字),以便在返回时轻松显示和执行返回操作我希望使用数字运算符来确定它与每个单个运算符的比较方式。

My testing has shown that non-primitives cannot be compared, unless you are using Comparable's compareTo(). 我的测试表明,除非使用Comparable的compareTo(),否则无法比较非原始元素。

I am tempted to convert the number to something less generic to give me the functionality I require but I am concerned about overflow and memory usage. 我很想将数字转换为不太通用的名称,以提供所需的功能,但我担心溢出和内存使用情况。 I was thinking of going BigDecimal but i know Floats have floating point precision problems. 我曾考虑过要使用BigDecimal,但我知道Float具有浮点精度问题。 How would the more experienced StackOverflow members handle this problem? 有经验的StackOverflow成员将如何处理此问题?

When a class implements comparable there's only one compareTo() method. 当一个类实现可比时,只有一个compareTo()方法。 You can't define two or more compareTo() methods in one class. 您不能在一个类中定义两个或多个compareTo()方法。 What if you want to compare some other values ? 如果要比较其他一些值怎么办? Then you can implement new class with comparator interface to use it with other values. 然后,您可以使用比较器接口实现新类,以将其与其他值一起使用。 Comparator interface comes to help in such case. 比较器界面可在这种情况下提供帮助。

Number is an abstract class - there's nothing to compare there. 数字是一个抽象类-没有可比的地方。 This class is extended by concrete classess like Integer, Double etc - this classess have concrete values to compare. 该类由诸如Integer,Double等的具体类扩展-该类具有要比较的具体值。

Here's a great example how to use both interfaces: http://javarevisited.blogspot.com/2011/06/comparator-and-comparable-in-java.html 这是一个很好的示例,说明如何同时使用两个接口: http : //javarevisited.blogspot.com/2011/06/comparator-and-comparable-in-java.html

Both of these questions have been asked numerous times before. 这两个问题以前都被问过无数次。 Remember that Google is your friend. 请记住,Google是您的朋友。

They differ in usage, logic and implementation. 它们在用法,逻辑和实现方面有所不同。

在此处输入图片说明

Comparison and examples can be found on this site . 比较和示例可以在此站点上找到。

As for the second part of the question, check out this SO question . 至于问题的第二部分,请查看此SO问题 It explains why the Number class doesn't implement the Comparable interface. 它解释了为什么Number类不实现Comparable接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM