Collection中移除失败问题

亚林瓜子 2011-12-02 10:05:21
我建立一个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
}

}
...全文
107 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
awusoft 2011-12-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 awusoft 的回复:]
先判断hashCode的,先把hashCode方法写上吧,你的代码是
public int hastCode()
应该是
public int hashCode()
[/Quote]

我不是告诉你这里的方法名不对了吗?
你方法名不一样,就相当于没有重写hashCode方法啊.调用的是Object的hashCode方法.
亚林瓜子 2011-12-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 huntor 的回复:]

hashCode 错成 hastCode了 。
[/Quote]
改掉之后,运行正常,两句打印均为true;
改进之前,这两句一个打印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

huntor 2011-12-02
  • 打赏
  • 举报
回复
hashCode 错成 hastCode了 。
亚林瓜子 2011-12-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 awusoft 的回复:]

先判断hashCode的,先把hashCode方法写上吧,你的代码是
public int hastCode()
应该是
public int hashCode()
[/Quote]
我将Name类中的hashCode方法重写,得到的结果还是一样的,仍然没有移除,上面一个打印true,下面一个打印false
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));
}

}

wang7535067 2011-12-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 awusoft 的回复:]
先判断hashCode的,先把hashCode方法写上吧,你的代码是
public int hastCode()
应该是
public int hashCode()
[/Quote]
正解
awusoft 2011-12-02
  • 打赏
  • 举报
回复
先判断hashCode的,先把hashCode方法写上吧,你的代码是
public int hastCode()
应该是
public int hashCode()
awusoft 2011-12-02
  • 打赏
  • 举报
回复

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方法

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧