请教电化学/化工专家# ChemEng - 化学工程
b*g
1 楼
All set cares uniqueness and equals() determine whether two object are
identical.
但是我test 了下
import java.util.HashSet;
public class testT {
private String name;
public testT(String name){
this.name=name;
}
public boolean equals(Object o){
if(!(o instanceof testT))return false;
testT person=(testT)o;
return person.name.equals(this.name);
}
public static void main(String[] args){
HashSet hs=new HashSet();
testT p1= new testT("jing");
testT p2=new testT("jing");
hs.add(p1);
hs.add(p2);
System.out.println(hs.size());
System.out.println(p1.equals(p2));
for(Object obj:hs)
System.out.println(obj);
}
}
输出是:
2
true
[email protected]
[email protected]
为啥两个object 被认为是equals的,缺还能加在同一个hashset里?
identical.
但是我test 了下
import java.util.HashSet;
public class testT {
private String name;
public testT(String name){
this.name=name;
}
public boolean equals(Object o){
if(!(o instanceof testT))return false;
testT person=(testT)o;
return person.name.equals(this.name);
}
public static void main(String[] args){
HashSet hs=new HashSet();
testT p1= new testT("jing");
testT p2=new testT("jing");
hs.add(p1);
hs.add(p2);
System.out.println(hs.size());
System.out.println(p1.equals(p2));
for(Object obj:hs)
System.out.println(obj);
}
}
输出是:
2
true
[email protected]
[email protected]
为啥两个object 被认为是equals的,缺还能加在同一个hashset里?