覆盖equals()以与Linked List一起使用

时间:2014-04-27 23:41:01

标签: java linked-list equals

我有两种方法可以查找链表中特定元素的索引。我正在尝试使用contais()indexOf()来查找索引。我不确定如何覆盖equals()以满足我的需要。一种方法根据姓氏和首字母查找索引,另一种方法是电话号码。以下是我的方法:

@Override
public int lookupNumber(String surname, String initials) {
    Entry entry1 = new Entry(surname, initials);
    if (listDirectory.contains(entry1)) {
        int index = listDirectory.indexOf(entry1);
        return index;
    }
    else {
        return -1;
    }
}
@Override
public int lookupName(int extension) {
    Entry entry1 = new Entry(Integer.toString(extension));
    if (listDirectory.contains(entry1)) {
        int index = listDirectory.indexOf(entry1);
        return index;
    }
    else {
        return -1;
    }
}

我尝试使用eclipse提供的equals()hashCode()

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Entry other = (Entry) obj;
    if (extension == null) {
        if (other.extension != null)
            return false;
    } else if (!extension.equals(other.extension))
        return false;
    if (initals == null) {
        if (other.initals != null)
            return false;
    } else if (!initals.equals(other.initals))
        return false;
    if (surname == null) {
        if (other.surname != null)
            return false;
    } else if (!surname.equals(other.surname))
        return false;
    return true;
}

但它对我的两个方法都不起作用,因为它比较了对象中的所有变量,而不仅仅是我需要它进行比较的变量。这是正确的逻辑是什么?

非常感谢,谢谢。

更新 - 这是我的完整入门课程 -

public class Entry { 

private String surname;
private String initals;
private String extension;



public Entry(String surname, String initals,String extension) { 
    this.surname = surname;
    this.initals = initals;
    this.extension = extension;
}


  public Entry(String surname,String initals){
    this.surname = surname;
    this.initals = initals;
}


public Entry(String extension) {
    this.extension = extension;
}

public String getInitals(){

    return initals;

}

public String getSurname(){

    return surname;
}

public String getExtension(){

    return extension;
}

public void setExtension(String extension) {
     this.extension = extension;
}

public String toString(){
    return surname  + "\t " + initals + "\t" + extension;

}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((extension == null) ? 0 : extension.hashCode());
    result = prime * result + ((initals == null) ? 0 : initals.hashCode());
    result = prime * result + ((surname == null) ? 0 : surname.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Entry other = (Entry) obj;
    if (extension == null) {
        if (other.extension != null)
            return false;
    } else if (!extension.equals(other.extension))
        return false;
    if (initals == null) {
        if (other.initals != null)
            return false;
    } else if (!initals.equals(other.initals))
        return false;
    if (surname == null) {
        if (other.surname != null)
            return false;
    } else if (!surname.equals(other.surname))
        return false;
    return true;
}

}

更新 - 我的问题出在我的lookupNumber()方法中,我希望按(initials,surname)找到元素,而在lookupNumber()我希望按(extension)查找元素。我该怎么做?

1 个答案:

答案 0 :(得分:0)

Java Object类包含自己的equals()方法,Entry类应该是Entry类型对象的蓝图。 Java继承是这样的,所有类都继承自Object类,这对我们很多人来说几乎没用。真的只需要

@Override
public boolean equals(Entry otherEntry)
{
    if(!(this.surname.equals(otherEntry.getSurname()))
        return false;
    else if(!(this.initials.equals(otherEntry.getInitials()))
        return false;
    else if(!(this.extension.equals(otherEntry.getExtension()))
        return false;
    else
        return true;
}

上面的代码只检查实例变量之间的不等式,在任何不相等的情况下返回false。