public class Name {
private String firstName,lastName;
public Name(String firstName,String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String toString()
{
return firstName+" "+lastName;
}
}import java.util.*;public class Basecontainer {
public static void main(String[] args) {
Collection c = new HashSet();
c.add("hello");
c.add(new Name("ds","as"));
c.add(new Integer(123));
System.out.println(c.remove(new Integer(123)));
//c.remove(new Integer(123));
System.out.println(c.remove(new Name("ds","as")));
System.out.print(c);
}两个类
用容器添加object运行结果为
true
false
[ds as, hello]
为什么第一个是true第二个是false呢?
private String firstName,lastName;
public Name(String firstName,String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String toString()
{
return firstName+" "+lastName;
}
}import java.util.*;public class Basecontainer {
public static void main(String[] args) {
Collection c = new HashSet();
c.add("hello");
c.add(new Name("ds","as"));
c.add(new Integer(123));
System.out.println(c.remove(new Integer(123)));
//c.remove(new Integer(123));
System.out.println(c.remove(new Name("ds","as")));
System.out.print(c);
}两个类
用容器添加object运行结果为
true
false
[ds as, hello]
为什么第一个是true第二个是false呢?
解决方案 »
- 一个简单的文本阅读器,求大神指教。
- 一个关于 更改图片的问题(务必指教)
- 处理异常的一个小问题!
- 通过什么软件,能将文本转换为二进制各市进行察看,比如UtralEdit 等工具!___特急
- 为大家谋福利的我给大家贴个面试题,是今天我一个软件公司面试的。
- 用MS1.1的JVM运行的APPLET是不是不能连接SQL-SERVER数据库?(急啊,发了一贴没人回啊
- 使用JCreator2.5的问题。
- 求教大虾!!! 如何在jdk1.3中使用ImageIO这个类。
- 菜鸟又来送分了!不胜感激!!!特急!!!!!!!!!!!!!!!!!
- 下列方法,在BMP实体Bean内何时被调用,应在各个方法内放置那些操作?请指教!
- 急:找不到 msvcr71.dll
- 我用URL來下載文件,速度很慢,怎麼解決?
而Name却是两个不同的对象
解决办法可以通过操作同一引用或如1楼
Integer b = new Integer(123)
Integer类实现的Object类的hashcode方法
所以a和b生成的hashcode是一样的,存在HashSet中只是一个引用
Name类没有实现hashcode方法
Name a = new Name("ds","as")
Name b = new Name("ds","as")
a和b的hashcode是a和b的地址
HashSet 中remove方法移除的对象,移除的时候也是根据hashcode操作的
所以你的结果为true,false
import java.util.*; class Name {
private String firstName,lastName;
public Name(String firstName,String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String toString()
{
return firstName+" "+lastName;
}
public boolean equals(Object obj) {
if (obj instanceof Name) {
Name n = (Name)obj;
return n.firstName.equals(firstName) && n.lastName.equals(lastName);
} else {
return false;
}
}
public int hashCode() {
return firstName.hashCode();//有带改进,这样写不规范
}
} public class Basecontainer {
public static void main(String[] args) {
Collection c = new HashSet();
c.add("hello");
c.add(new Name("ds","as"));
c.add(new Integer(123));
System.out.println(c.remove(new Integer(123)));
//c.remove(new Integer(123));
System.out.println(c.remove(new Name("ds","as")));
System.out.print(c);
} }