package com.oristand.program;
public class Name {
String fristName, lastName;
public Name() {
}
public Name(String string, String string2) {
fristName = string;
lastName = string2;
}
public String toString() {
return (fristName + " " + lastName);
}
public int hashCode() {
return 31*fristName.hashCode()+lastName.hashCode();
}
public boolean equals(Object obj) {
Name name = new Name();
if (obj instanceof Name) {
return (fristName.equals(name.fristName))
&& (lastName.equals(name.lastName));
}
return obj.equals(name);
}
}
////////////////////////////////////////////////////////////////////////
package com.oristand.program;
import java.util.*;
public class TestHashSet {
public static void main(String[] args) {
Collection<Object> c = new LinkedList<Object>();
c.add("hello");
c.add(100);
c.add(new Name("f1", "l1"));
c.add(new Integer(100));
System.out.println(c);
c.remove("hello");
c.remove(new Integer(100));
c.remove(new Name("f1", "l1"));//为什么这里会删不掉
System.out.println();
System.out.println(c);
}
}
public class Name {
String fristName, lastName;
public Name() {
}
public Name(String string, String string2) {
fristName = string;
lastName = string2;
}
public String toString() {
return (fristName + " " + lastName);
}
public int hashCode() {
return 31*fristName.hashCode()+lastName.hashCode();
}
public boolean equals(Object obj) {
Name name = new Name();
if (obj instanceof Name) {
return (fristName.equals(name.fristName))
&& (lastName.equals(name.lastName));
}
return obj.equals(name);
}
}
////////////////////////////////////////////////////////////////////////
package com.oristand.program;
import java.util.*;
public class TestHashSet {
public static void main(String[] args) {
Collection<Object> c = new LinkedList<Object>();
c.add("hello");
c.add(100);
c.add(new Name("f1", "l1"));
c.add(new Integer(100));
System.out.println(c);
c.remove("hello");
c.remove(new Integer(100));
c.remove(new Name("f1", "l1"));//为什么这里会删不掉
System.out.println();
System.out.println(c);
}
}
解决方案 »
- 菜题,抛出异常问题
- 为什么我运行第二个程序的时候只显示一个窗框而已呢?
- 初学JAVA,弱弱的问2个问题。
- 关于servlet和线程的问题
- 小弟初学者,请问j2ee连接数据库必须使用JDBC并且必须设置odbc数据源吗?
- 为什么点击一个menu没有反应,而用按钮或者设定该menu的mouseEntered事件都可以执行相关的代码的呢/相关代码段如下:
- 这问题怎么解决???
- 为什么"ExplicitStatic.java"报错,该引用哪个包??等
- 我好像发现了序列化对象的一个bug!
- socket传文件,如何设置合理的缓存?
- Hibernate与Spring 问题
- 为什么将JAVA数据写入数据库(ORACLE)中的表,在ORACLE中找不出写入的数据???
String fristName, lastName;
public Name() {
}
public Name(String string, String string2) {
fristName = string;
lastName = string2;
}
public String toString() {
return (fristName + " " + lastName);
}
public int hashCode() {
return 31*fristName.hashCode()+lastName.hashCode();
}
public boolean equals(Object obj) {
Name name;
if (obj instanceof Name) {
name=(Name)obj;
//obj的类型要转换成Name,在你的代码里name没有内容
return (fristName.equals(name.fristName))
&& (lastName.equals(name.lastName));
}
//return obj.equals(name);
return false;
}
}
好象你那样的强转是否不行吧obj强转到自定义类是否不对,
public boolean remove(Object o) {
......
if (o.equals(e.element)) {
remove(e);
return true;
}
......
用到了输入对象中的equals方法来比较对象.看一下LZ的此代码片段:
public boolean equals(Object obj) {
Name name = new Name();
if (obj instanceof Name) {
return (fristName.equals(name.fristName))
&& (lastName.equals(name.lastName));
}
return obj.equals(name);
}
name 为新实例(见绿色部分)在new时调用其无参构造函数,并没有给fristName赋值.name.fristName 为null,
其中红色部分中fristName 为 "l1" 此return永远返回false.
所以使用c.remove(new Name("f1", "l1"));不会删除其c中的元素.