java Java中用“<”或“>”运算符比较两个对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29179194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare two objects with "<" or ">" operators in Java
提问by IvanN
How to make two objects in Java comparable using "<" or ">" e.g.
如何使用“<”或“>”使Java中的两个对象具有可比性,例如
MyObject<String> obj1= new MyObject<String>(“blablabla”, 25);
MyObject<String> obj2= new MyObject<String>(“nannaanana”, 17);
if (obj1 > obj2)
do something.
I've made MyObject class header as
我已将 MyObject 类标题设为
public class MyObject<T extends Comparable<T>> implements Comparable<MyObject<T>>
and created method Comp but all the gain I got is now I can use "sort" on the list of objects, but how can I compare two objects to each other directly? Is
public class MyObject<T extends Comparable<T>> implements Comparable<MyObject<T>>
并创建了 Comp 方法,但我现在获得的所有收益都是我可以在对象列表上使用“排序”,但是如何直接比较两个对象?是
if(obj1.compareTo(obj2) > 0)
do something
the only way?
唯一的办法?
回答by Anderson Vieira
You cannot do operator overloading in Java. This means you are not able to define custom behaviors for operators such as +
, >
, <
, ==
, etc. in your own classes.
您不能在 Java 中进行运算符重载。你是这意味着无法定义特定行为的运营商,如+
,>
,<
,==
,等在自己的类。
As you already noted, implementing Comparable
and using the compareTo()
method is probably the way to go in this case.
正如您已经指出的那样,在这种情况下可能是实现Comparable
和使用该compareTo()
方法的方法。
Another option is to create a Comparator
(see the docs), specially if it doesn't make sense for the class to implement Comparable
or if you need to compare objects from the same class in different ways.
另一种选择是创建一个Comparator
(参见文档),特别是如果类实现没有意义,Comparable
或者如果您需要以不同方式比较来自同一类的对象。
To improve the code readability you could use compareTo()
together with custom methods that may look more natural. For example:
为了提高代码可读性,您可以compareTo()
与看起来更自然的自定义方法一起使用。例如:
boolean isGreaterThan(MyObject<T> that) {
return this.compareTo(that) > 0;
}
boolean isLessThan(MyObject<T> that) {
return this.compareTo(that) < 0;
}
Then you could use them like this:
然后你可以像这样使用它们:
if (obj1.isGreaterThan(obj2)) {
// do something
}
回答by Elliott Frisch
Using Comparable.compareTo(T)
is the only option (or Comparator
). The interface only defines that one method (while Comparator
adds equals
), and it compares this object with the specified object for order.Further, Java does not permit operator overloading (so you won't be able to directly change the operand used for invoking that method; or in fact modify the interface).
使用Comparable.compareTo(T)
是唯一的选择(或Comparator
)。该接口只定义了一个方法(while Comparator
adds equals
),并将此对象与指定的对象进行比较以进行排序。此外,Java 不允许运算符重载(因此您将无法直接更改用于调用该方法的操作数;或者实际上修改接口)。
回答by user3437460
It is not the only way. You can implement a Comparator as well. Comparator uses compare()
method as oppose to Comparable
which uses compareTo()
method.
这不是唯一的方法。您也可以实现 Comparator。比较器使用compare()
方法,反对Comparable
其使用compareTo()
方法。
The reason you can't use >
or <
to compare objects directly is because Java won't know which variable you want to use for the comparison (as there might exist more than one variable in the object).
您不能直接使用>
或<
比较对象的原因是 Java 不知道您要使用哪个变量进行比较(因为对象中可能存在多个变量)。
In order to compare objects, those objects must be comparable. You need to define and tell Java how you want to compare them.
为了比较对象,这些对象必须是可比较的。您需要定义并告诉 Java 您想如何比较它们。
Java collection provides a sort
method. However some school does give assignment of asking you to write you own sort methods which ultimately still uses the compareTo()
for comparison.
Java集合提供了一种sort
方法。但是,有些学校确实会要求您编写自己的排序方法,这些方法最终仍然使用compareTo()
进行比较。
You can take a look on the subtle differences between Comparable
vs Comparator
here: What is the difference between compare() and compareTo()?
您可以在此处查看Comparable
vs之间的细微差别Comparator
:compare() 和 compareTo() 之间有什么区别?
I think it is also worth mentioning that, by default Java compares String (objects) in a lexicographical order if you did not override the compareTo()
method.
我认为还值得一提的是,默认情况下,如果您没有覆盖该compareTo()
方法,Java 会按字典顺序比较 String(对象)。