import java.util.*;
public class Test{
public static void main(String[] args){
Collection c = new HashSet();
c.add("Hello");
c.add(new Name("f1","11"));
c.add(new Integer(100));
System.out.println(c);
c.remove("Hello");
System.out.println(c);
c.remove(new Integer(100));
System.out.println(c);
System.out.println(c.remove(new Name("f1","11")));
//为什么结果是false,希望大虾们讲下remove方法的实现过程。 System.out.println(c);
}
}
class Name{
String s;
String s1;
Name(String A,String B){
this.s = A;
this.s1 = B;
}
public String toString(){
return s+" "+s1;
}}程序结果
[100, f1 11, Hello]
[100, f1 11]
[f1 11]
false
[f1 11]
希望讲解下remove方法的细节,我觉得应该输出的是ture ,问什么不能移去Name呢?
public class Test{
public static void main(String[] args){
Collection c = new HashSet();
c.add("Hello");
c.add(new Name("f1","11"));
c.add(new Integer(100));
System.out.println(c);
c.remove("Hello");
System.out.println(c);
c.remove(new Integer(100));
System.out.println(c);
System.out.println(c.remove(new Name("f1","11")));
//为什么结果是false,希望大虾们讲下remove方法的实现过程。 System.out.println(c);
}
}
class Name{
String s;
String s1;
Name(String A,String B){
this.s = A;
this.s1 = B;
}
public String toString(){
return s+" "+s1;
}}程序结果
[100, f1 11, Hello]
[100, f1 11]
[f1 11]
false
[f1 11]
希望讲解下remove方法的细节,我觉得应该输出的是ture ,问什么不能移去Name呢?
你这是新对象
如果你需要让 属性相同的 对象在 collection 中算同一个对象的话
去重写 Name 的 hashcode 和 equels 方法
o - 要从此 collection 中移除的元素(如果存在)。
返回:
如果此调用将移除一个元素,则返回 true
看完这个 LZ应该明白一切了吧!
就是看了这个还是不明白。按照这个说法o==null不满足执行o.equals(e)) 这个e指的事什么?
这个应该是相等的啊!是ture 问什么是false呢?
remove方法的实现过程是什么样的,它调用equals和hashcode方法,我没重写。这个是后它调用的是哪的equals和hashcode方法呢?
remove方法的实现过程是什么样的-- 这你看HashSet的源码就好了。
我没重写。这个是后它调用的是哪的equals和hashcode方法呢? --- 调用Object中默认的equals和hashcode
你new name就是新对象了
import java.util.*;
public class Test{
public static void main(String[] args){
Collection c = new HashSet();
Name n = new Name("f1","11");//声明一个n
c.add("Hello");
c.add(new Name("f1","11"));
c.add(n);
System.out.println(c);
c.remove("Hello");
System.out.println(c);
c.remove(new Integer(100));
System.out.println(c);
System.out.println(c.remove(n));//不new
System.out.println(c);
}
}
class Name{
String s;
String s1;
Name(String A,String B){
this.s = A;
this.s1 = B;
}
public String toString(){
return s+" "+s1;
}}
结果
[f1 11, f1 11, Hello]
[f1 11, f1 11]
[f1 11, f1 11]
true
[f1 11]问什么c总还会打印出[f1 11] 没有删去吗?
这个没删不是很正常的吗
你只是删除了 n
你的 n 虽然 和后来 new Name("f1","11") 的对象属性一样
但他们不是同一个对象
c.add("Hello");
c.add(new Name("f1","11"));
你这里如果c.add(n);
你估计就可以remove了Name n = new Name("f1","11");//
c.add(new Name("f1","11"));
这里是不同的Name
c.add(new Name("f1","11"));
c.add(n);
你不觉得应该是加入了2个有相同属性的 Name 对象吗
然后
c.remove(n);
这个只是删了 n 而已
public class Test{
public static void main(String[] args){
Collection c = new HashSet();
c.add("Hello");
c.add(new Name("f1","11"));
c.add(new Integer(100));
System.out.println(c);
c.remove("Hello");
System.out.println(c);
c.remove(new Integer(100));
System.out.println(c);
System.out.println(c.remove(new Name("f1","11")));
//为什么结果是false,希望大虾们讲下remove方法的实现过程。 System.out.println(c);}
}
class Name{
String s;
String s1;
Name(String A,String B){
this.s = A;
this.s1 = B;}
public String toString(){
return s+" "+s1;
}}结果
[100, f1 11, Hello]
[100, f1 11]
[f1 11]
true
[]这样就对了,
谢谢大家的帮助!