您的位置:首页 > 其它

scala中==,equals,eq与ne比较

2017-05-25 22:16 495 查看
记录一下scala中常用的几种比较方法的用法

“==”方法的使用及含义

首先看一下官方文档给的解释:

final def ==(arg0: Any): Boolean
//测试两个对象是否相等。表达式x==that含义为:如果x eq null 则为that eq null 否则为x.equals(that)
Test two objects for equality. The expression x == that is equivalent to if (x eq null) that eq null else x.equals(that).
returns true if the receiver object is equivalent to the argument; false otherwise.


可以看到==含义中切分为两部分:eq与equals,具体要判断x是否为null,要更进一步理解==需要接着往下看equals与eq的含义

equals方法的使用及含义

官方文档解释:

***def equals(arg0: Any): Boolean***
Compares the receiver object (this) with the argument object (that) for equivalence.
Any implementation of this method should be an equivalence relation:
It is reflexive: for any instance x of type Any, x.equals(x) should return true.
It is symmetric: for any instances x and y of type Any, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any instances x, y, and z of type Any if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
If you override this method, you should verify that your implementation remains an equivalence relation. Additionally, when overriding this method it is usually necessary to override hashCode to ensure that objects which are "equal" (o1.equals(o2) returns true) hash to the same scala.Int. (o1.hashCode.equals(o2.hashCode)).
returns true if the receiver object is equivalent to the argument; false otherwise.


上面一大堆英文总的来说有几方面意思:

equals方法具有几个性质:自反性,对称性,传递性

如果你需要重写equals方法,那么也必须要重写对象对应的hashCode方法,且如果对象1equals对象2的话,那么这两个对象的hashCode也应该相等

equals方法比较的是值是否相等

eq方法的使用及含义

官方文档解释:

***final def eq(arg0: AnyRef): Boolean***
Tests whether the argument (that) is a reference to the receiver object (this).

The eq method implements an equivalence relation on non-null instances of AnyRef, and has three additional properties:

It is consistent: for any non-null instances x and y of type AnyRef, multiple invocations of x.eq(y) consistently returns true or consistently returns false.
For any non-null instance x of type AnyRef, x.eq(null) and null.eq(x) returns false.
null.eq(null) returns true.
When overriding the equals or hashCode methods, it is important to ensure that their behavior is consistent with reference equality. Therefore, if two objects are references to each other (o1 eq o2), they should be equal to each other (o1 == o2) and they should hash to the same value (o1.hashCode == o2.hashCode).
returns
true if the argument is a reference to the receiver object; false otherwise.


简而言之,这里eq方法进行的是引用比较,比较两个对象的引用是否相同

ne方法的使用及其含义

官方解释:

***final def ne(arg0: AnyRef): Boolean***
Equivalent to !(this eq that).
returns true if the argument is not a reference to the receiver object; false otherwise.


可以看出来,ne方法是eq方法的反义

总结

综合上面几种方法的阐述,可以看出来

如果要比较对象的引用是否相同或者不同,请用eq或ne方法

如果要比较值是否相等,请用equals方法或者==方法,这里推荐使用==方法,因为如果比较值为null的情况下,调用equals方法是会报错的,而==方法则避免了这个问题,它事先为我们检查了是否为null,然后在进行相应比较
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  scala