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);
}
}
解决方案 »
- 为什么同一个文件中含有【2个public class】就无法编译呢?
- 关于java调用mysqldump的问题
- 一个非常奇怪的函数重写 FileNotFoundException
- 新手学JAVA的疑惑!(是不是要学C才能学好JAVA?请前辈们指点!)
- 高手帮我看两个后台都是什么意思(求助)
- 请教一个log4j不同的输出方式,输出不同级别log的问题
- equals和==有什么区别?迷惑中.
- java中的组件布局问题
- 哪位大哥可以整理一下javamail的有关问题吗,我愿意以分相赠
- 请问有没有将class文件转化成exe文件的工具?
- 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中的元素.