Item 8: Obey the general contract when overriding equals
Equivalence relation(equals必须满足的性质):
Reflexive: For any non-null reference value x, x.equals(x) must return true.
Symmetric: For any non-null reference values x and y, x.equals(y) must return true if and only if y.equals(x) returns true.
Transitive: For any non-null reference values x, y, z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) must return true.
Consistent: For any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) must return false.
不满足对称性的例子:
Once you’ve violated the equals contract, you simply don’t know how other objects will behave when confronted with your object.
为了消除该问题,必须避免让CaseInsensitiveString和String进行比较,代码如下:
另一个例子: 现在需要继承这个Point,并加上Color属性如下: 这样写违反了对称性(用一个Point与ColorPoint比较如:Point p = new Point(1, 2); ColorPoint cp = new ColorPoint(1, 2, Color.RED);)。改写equals如下: 这样写避免了Point与ColorPoint比较时造成的对称性违反。但是违反了传递性:事实上,在这种情况下无法实现满足这些性质的equals方法。There is no way to extend aninstantiable class and add a value component while preserving the equalscontract。这里的解决方法是:Item 16.Favorcomposition over inheritance.代码如下:在Java类库中,java.sql.Timestampextends java.util.Date and adds a nanoseconds field.The equals implementationfor Timestamp does violate symmetry.将两者同时用在一个Collection中会导致错误。
recipe for a high-quality equals method:
1. Use the == operator to check if the argument is a reference to this object.
2. Use the instanceof operator to check if the argument has the correct type.
3. Cast the argument to the correct type.
4. For each “significant” field in the class, check if that field of the argument matches the corresponding field of this object.
primitive fields whose type is not float or double : ==
float/double fields:Float.compare/Double.compare(for Float.NaN, -0.0f)
object reference fields : equals
array:Arrays.equals
Some object reference fields may legitimately contain null.To avoid the possibility of a NullPointerException, use this idiom to compare such fields: (field == null ? o.field == null : field.equals(o.field))
5. When you are finished writing your equals method, ask yourself three questions: Is it symmetric? Is it transitive? Is it consistent?
分享到:
相关推荐
Java Property Utils是Java开发中的一个实用工具库,主要用于处理和操作Java系统属性和配置文件。在给定的场景中,`java-property-utils-1.9.1.jar` 和 `java-property-utils-1.10.jar` 是这个库的两个不同版本,...
在这个场景中,我们关注的两个jar包——"cors-filter-1.7.jar"和"java-property-utils-1.9.jar"与Geoserver的安装以及CORS配置紧密相关。 首先,让我们了解Geoserver。Geoserver是一款开源的地理信息系统服务器,...
2. 创建一个配置文件,例如`CORSFilter.properties`,其中定义跨域策略。比如: ``` allowed.origins=http://example.com, https://api.example.com allowed.methods=GET, POST, PUT, DELETE, OPTIONS allowed....
Spring Data Commons 和 Spring Data JPA 是 Spring 框架中的两个关键模块,它们在 Java 应用开发中扮演着重要角色,特别是在数据访问层。这两个模块是基于 Java 的数据访问接口(JPA)来简化数据库操作,使得开发者...
2. Java Property Utils: `java-property-utils`库提供了一组工具类,用于处理Java的属性文件(.properties)。这些工具可能包括读取、写入、解析和格式化属性文件的功能。在配置CORS Filter时,我们可能需要从...
import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;...
2. **定义CORS策略**:配置过滤器时,需要指定允许的源(Origins)、允许的方法(Methods)、允许的头部(Headers)、以及是否允许携带凭证(Credentials)等。 3. **测试验证**:部署应用后,使用不同源的浏览器...
|--- Essentials.of.Mathematica.-.With.App.tics.and.Physics.pdf |--- Introduction.to.Maple.for.Physics.St.Maple.Powertools.pdf |--- Introduction.to.Mathematics.With.Maple.pdf |--- Learning.by....
additional-methods.min.js是微信平台二次开发系统
mysql-connector-java-5.1.40-bin.jar ognl-3.0.6.jar servlet-api.jar spring-aop-4.0.0.RELEASE.jar spring-beans-4.0.0.RELEASE.jar spring-context-4.0.0.RELEASE.jar spring-core-4.0.0.RELEASE....
2. **Java Property Utils** (java-property-utils-1.9.1.jar): 这个库提供了处理Java系统属性和环境变量的工具类。在实现CORS过滤器时,可能需要读取或设置配置参数,比如CORS策略的白名单域名、允许的方法等。`...
This option will switch from fast-methods to slower methods, but which take less memory. Use 0 (default) for small projects, use 1 for large projects (if ecc32 runs out of memory). 2)....Added: --el_...
《JAVA中使用HttpClient:commons-httpclient-3.0.jar详解》 在JAVA开发中,进行HTTP请求时,Apache的HttpClient库是一个不可或缺的工具。本文将深入解析`commons-httpclient-3.0.jar`,它是HttpClient的一个重要...
Java 属性工具库(java-property-utils-1.9.1.zip) Java 属性工具库(java-property-utils)是一个用于处理 Java 应用程序属性的实用程序库,版本为 1.9.1。这个库提供了方便的方法来读取、解析、操作和管理 Java ...
Good luck to all of you who will take it from here and write some nice applications to use these methods more efficiently we anxiously await your mail. This would be possible with our Mosb3rg and ...
- Multi-language architecture allows you to use many languages (Pascal, C++, Basic, Java). - Access to any object inside your application (in case you've allowed this). Standard libraries to ...
`java-property-utils-1.9.jar` 是一个Java库,主要用于处理和操作Java的属性文件。在Java中,属性文件通常用于存储配置信息,如数据库连接字符串、系统参数等。`java-property-utils` 提供了方便的方法来读取、写入...