简体   繁体   中英

Best way to compare different elements in equals() method for custom LinkedList

In my class below I have 2 strings and an equals method for my custom LinkedList. Now I know how to compare different objects in my equals method but say I wanted to compare name earlier in my programme before I compared number how would I type this in my equals() method. Below I tried an else if but I know this wont work as number will be comparing itself to another number when I only want it be comparing a name and the same later in my programme when I only want to compare a number and not a name, if that makes sense. Below is my class and what I tried for my equals method. Any help would be greatly appreciated:) PS sorry class is called Id im not very good with stack overflow and could not get it to show in the code section

private String number;
private String name;

public Id(){
    this.number = "";
    this.name = "";
    
    
}
    public Id(String number,String name){
        this.number = number;
        this.name = name;
    
}


public String getName(){
    
    return this.name;
}

public void setName(String name){
    this.name = name;
}
public String getNumber(){
    
    return this.number;
}

public void setNumber(String number){
    this.number = number;
}

@Override
public boolean equals(Object o){
     Id i = (Id)o;
if(this.number.equalsIgnoreCase(e.getNumber())){
    return true;

}else if(this.name.equalsIgnoreCase(e.getName()))
 return true;
else 
    return false;

'''

That equals method is broken.

  1. It should not throw. In particular, if you attempt to ask it: Hey, are you equal to this apple, it should answer 'no'. Your code will 'answer' with a ClassCastException.

  2. Your code will return true if the number is equal to the other number, __OR__ if the name is equal to the other name . That's a bizarre definition. Surely it should be: Both . That's a bizarre definition. Surely it should be: Both . That's a bizarre definition. Surely it should be: Both number and name` should be equal.

  3. You can't override only equals, you must always override both equals and hashCode . If you fail to do this, using this object for example as keys in a HashMap results in utterly bizarre behaviour, where keys you add seem to just disappear into thin air.

  4. If you want an equals method that only compares numbers and not the name, you can do that, you can write whatever you want. But, an equals method must be commutative, associative and adhere to identity. In other words, a.equals(a) must be true, if a.equals(b) , then b.equals(a) must be true, and if a.equals(b) && b.equals(c) , then a.equals(c) must be true. Again, the compiler won't stop you if you fail to adhere to these rules, but various classes rely on your equals method working like this, and if it doesn't, they'll act in bizarre ways.

If your goal really was: Any 2 IDs are equal if name matches name OR number matches number - then your method mostly does that, though the cast is wrong, you have to add:

if (this == o) return true;
if (o == null) return false;
if (o.getClass() != this.getClass()) return false;
Id other = (Id) o;
// carry on comparing members here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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