我建立一个Name类,Name类已经重写了equals方法。在测试(BasicContainer类)中把Name放入Collection中然后调用remove方法进行移除,但移除失败
Name类:
package day07;public class Name implements Comparable {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
/*public int hastCode(){
return this.getFirstName().hashCode();
}*/
@Override
/**
* 重写的equals方法在这里
*/
public boolean equals(Object obj){
if(obj instanceof Name){
Name name = (Name) obj;
return (this.getFirstName().equals(name.getFirstName())
&& this.getLastName().equals(name.getLastName()));
}
return false;
}
@Override
public String toString() {
return firstName + "" + lastName;
}
@Override
public int compareTo(Object o) {
Name n = (Name)o;
int lastCmp = lastName.compareTo(n.lastName);
return (lastCmp != 0 ? lastCmp: firstName.compareTo(n.firstName));
}}
BasicContainerf类:
package day07;import java.util.Collection;
import java.util.HashSet;public class BasicContainer { /**
*equals比较内容
* @param args
*/
public static void main(String[] args) {
Collection c = new HashSet();
c.add("hello");
c.add("abc");
c.add(new Name("f1", "L1"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(new Name("f1","L1").equals((new Name("f1", "L1"))));//但这里为true
System.out.println(c.remove(new Name("f1", "L1")));//移除不掉,这里为false
System.out.println(c);//c中仍有f1L1
}}
Name类:
package day07;public class Name implements Comparable {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
/*public int hastCode(){
return this.getFirstName().hashCode();
}*/
@Override
/**
* 重写的equals方法在这里
*/
public boolean equals(Object obj){
if(obj instanceof Name){
Name name = (Name) obj;
return (this.getFirstName().equals(name.getFirstName())
&& this.getLastName().equals(name.getLastName()));
}
return false;
}
@Override
public String toString() {
return firstName + "" + lastName;
}
@Override
public int compareTo(Object o) {
Name n = (Name)o;
int lastCmp = lastName.compareTo(n.lastName);
return (lastCmp != 0 ? lastCmp: firstName.compareTo(n.firstName));
}}
BasicContainerf类:
package day07;import java.util.Collection;
import java.util.HashSet;public class BasicContainer { /**
*equals比较内容
* @param args
*/
public static void main(String[] args) {
Collection c = new HashSet();
c.add("hello");
c.add("abc");
c.add(new Name("f1", "L1"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(new Name("f1","L1").equals((new Name("f1", "L1"))));//但这里为true
System.out.println(c.remove(new Name("f1", "L1")));//移除不掉,这里为false
System.out.println(c);//c中仍有f1L1
}}
final Entry<K,V> removeEntryForKey(Object key) {
int hash = (key == null) ? 0 : hash(key.hashCode());
int i = indexFor(hash, table.length);
Entry<K,V> prev = table[i];
Entry<K,V> e = prev; while (e != null) {
Entry<K,V> next = e.next;
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k)))) {
modCount++;
size--;
if (prev == e)
table[i] = next;
else
prev.next = next;
e.recordRemoval(this);
return e;
}
prev = e;
e = next;
} return e;
}
HashSet的remove方法
public int hastCode()
应该是
public int hashCode()
Name类:
package day07;public class Name implements Comparable {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public int hastCode(){
return this.getFirstName().hashCode();
}
@Override
/**
* 重写的equals方法在这里
*/
public boolean equals(Object obj){
if(obj instanceof Name){
Name name = (Name) obj;
return (this.getFirstName().equals(name.getFirstName())
&& this.getLastName().equals(name.getLastName()));
}
return false;
}
@Override
public String toString() {
return firstName + "" + lastName;
}
@Override
public int compareTo(Object o) {
Name n = (Name)o;
int lastCmp = lastName.compareTo(n.lastName);
return (lastCmp != 0 ? lastCmp: firstName.compareTo(n.firstName));
}}
改进之前,这两句一个打印true,一个打印false,为什么效果不相同? System.out.println(new Name("f1","L1").equals((new Name("f1", "L1"))));//但这里为true
System.out.println(c.remove(new Name("f1", "L1")));//移除不掉,这里为false
我不是告诉你这里的方法名不对了吗?
你方法名不一样,就相当于没有重写hashCode方法啊.调用的是Object的hashCode方法.