分享
 
 
 

覆盖equals【引自http://www.javapractices.com/Topic17.cjp】

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Implementing equals

Discussion:

All objects have both identity (the object's location in memory) and state (the object's data). The == operator always compares identity. The default implementation of equals compares identity as well. Sometimes the default implementation of equals has the desired behaviour (as in a type-safe enumeration, for example), but equals should usually compare state, not identity. This is particularly true for "data-centric" classes which map to database records.

hashCode and equals are closely related :

if you override equals, you must override hashCode hashCode must generate equal values for equal objects Objects placed in a List , Set, or Map (as either a key or value) should have an appropriate definition of equals. (See, for example, the javadoc for Collection.contains , Map.containsKey, and Map.containsValue .) If you extend a concrete class, and add a new field which contributes to equals, then it is not possible to write a perfectly correct equals method for the new class. Instead, you should use composition instead of inheritance.

Example

Here is an implementation of equals for a data-centric class. It demonstrates how different types of fields are treated:

object fields, including collections : use equals type-safe enumerations : use either equals or == (they amount to the same thing, in this case) possibly-null object fields : use both == and equals array fields : use Arrays.equals primitive fields other than float or double : use == float : convert to int using Float.floatToIntBits, then use == double : convert to long using Double.doubleToLongBits, then use == It is worth noting that if fields are implemented with wrapper classes (Integer, Boolean, and so on), then implementation of equals is simpler, since there is only one case : calling the equals method recursively. (The compareTo method is also simplified in this case.) The above policies can be collected in a utility class :

import java.util.Arrays;

/**

* Collected methods which allow easy implementation of <code>equals</code>.

*

* Example use case in a class called Car:

* <pre>

public boolean equals(Object that){

if ( this == that ) return true;

if ( !(that instanceof Car) ) return false;

Car thatCar = (Car)that;

return

EqualsUtil.areEqual(this.fName, that.fName) &&

EqualsUtil.areEqual(this.fNumDoors, that.fNumDoors) &&

EqualsUtil.areEqual(this.fGasMileage, that.fGasMileage) &&

EqualsUtil.areEqual(this.fColor, that.fColor) &&

Arrays.equals(this.fMaintenanceChecks, that.fMaintenanceChecks); //array!

}

* </pre>

*

* <em>Arrays are not handled by this class</em>.

* This is because the <code>Arrays.equals</code> methods should be used for

* array fields.

*/

public final class EqualsUtil {

static public boolean areEqual(boolean aThis, boolean aThat){

//System.out.println("boolean");

return aThis == aThat;

}

static public boolean areEqual(char aThis, char aThat){

//System.out.println("char");

return aThis == aThat;

}

static public boolean areEqual(long aThis, long aThat){

/*

* Implementation Note

* Note that byte, short, and int are handled by this method, through

* implicit conversion.

*/

//System.out.println("long");

return aThis == aThat;

}

static public boolean areEqual(float aThis, float aThat){

//System.out.println("float");

return Float.floatToIntBits(aThis) == Float.floatToIntBits(aThat);

}

static public boolean areEqual(double aThis, double aThat){

//System.out.println("double");

return Double.doubleToLongBits(aThis) == Double.doubleToLongBits(aThat);

}

/**

* Possibly-null object field.

*

* Includes type-safe enumerations and collections, but does not include

* arrays. See class comment.

*/

static public boolean areEqual(Object aThis, Object aThat){

//System.out.println("Object");

return aThis == null ? aThat == null : aThis.equals(aThat);

}

}

Car is a class which uses EqualsUtil to implement its equals method :

import java.util.*;

public final class Car {

public Car (

String aName,

int aNumDoors,

List<String> aOptions,

double aGasMileage,

String aColor,

Date[] aMaintenanceChecks

){

fName = aName;

fNumDoors = aNumDoors;

fOptions = new ArrayList<String>(aOptions);

fGasMileage = aGasMileage;

fColor = aColor;

fMaintenanceChecks = new Date[aMaintenanceChecks.length];

for (int idx=0; idx < aMaintenanceChecks.length; ++idx) {

fMaintenanceChecks[idx] = new Date( aMaintenanceChecks[idx].getTime() );

}

}

public boolean equals(Object aThat) {

//check for self-comparison

if ( this == aThat ) return true;

//use instanceof instead of getClass here for two reasons

//1. if need be, it can match any supertype, and not just one class;

//2. it renders an explict check for "that == null" redundant, since

//it does the check for null already - "null instanceof [type]" always

//returns false. (See Effective Java by Joshua Bloch.)

if ( !(aThat instanceof Car) ) return false;

//Alternative to the above line :

//if ( aThat == null || aThat.getClass() != this.getClass() ) return false;

//cast to native object is now safe

Car that = (Car)aThat;

//now a proper field-by-field evaluation can be made

return

EqualsUtil.areEqual(this.fName, that.fName) &&

EqualsUtil.areEqual(this.fNumDoors, that.fNumDoors) &&

EqualsUtil.areEqual(this.fOptions, that.fOptions) &&

EqualsUtil.areEqual(this.fGasMileage, that.fGasMileage) &&

EqualsUtil.areEqual(this.fColor, that.fColor) &&

Arrays.equals(this.fMaintenanceChecks, that.fMaintenanceChecks);

}

//..other methods elided

// PRIVATE ////

/**

* The following fields are chosen to exercise most of the different

* cases.

*/

private String fName;

private int fNumDoors;

private List<String> fOptions;

private double fGasMileage;

private String fColor; //treat as possibly-null

private Date[] fMaintenanceChecks;

/**

* Exercise the equals method.

*/

public static void main (String[] aArguments) {

List<String> options = new ArrayList<String>();

options.add("sunroof");

Date[] dates = new Date[1];

dates[0] = new Date();

//Create a bunch of Cars; only one and two should be equal

Car one = new Car("Nissan", 2, options, 46.3, "Green", dates);

//two is equal to one

Car two = new Car("Nissan", 2, options, 46.3, "Green", dates);

//three has a differs in fName only

Car three = new Car("Pontiac", 2, options, 46.3, "Green", dates);

//four differs in fNumDoors only

Car four = new Car("Nissan", 4, options, 46.3, "Green", dates);

//five differs in fOptions only

List<String> optionsTwo = new ArrayList<String>();

optionsTwo.add("air conditioning");

Car five = new Car("Nissan", 2, optionsTwo, 46.3, "Green", dates);

//six differs in fGasMileage only

Car six = new Car("Nissan", 2, options, 22.1, "Green", dates);

//seven differs in fColor only

Car seven = new Car("Nissan", 2, options, 46.3, "Fuchsia", dates);

//eight differs in fMaintenanceChecks only

Date[] datesTwo = new Date[1];

datesTwo[0] = new Date(1000000);

Car eight = new Car("Nissan", 2, options, 46.3, "Green", datesTwo);

System.out.println( "one = one: " + one.equals(one) );

System.out.println( "one = two: " + one.equals(two) );

System.out.println( "two = one: " + two.equals(one) );

System.out.println( "one = three: " + one.equals(three) );

System.out.println( "one = four: " + one.equals(four) );

System.out.println( "one = five: " + one.equals(five) );

System.out.println( "one = six: " + one.equals(six) );

System.out.println( "one = seven: " + one.equals(seven) );

System.out.println( "one = eight: " + one.equals(eight) );

System.out.println( "one = null: " + one.equals(null) );

}

}

An example run of this class demonstrates that only objects one and two are equal :

one = one: true

one = two: true

two = one: true

one = three: false

one = four: false

one = five: false

one = six: false

one = seven: false

one = eight: false

one = null: false

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有