String a = new String("a");
String b = new String("b");
String a2 = new String("a");
Collection col = new HashSet();
col.add(a);
col.add(b);
// col.add(a2);
a = "";
col.remove(a);
// col.remove(a2);
System.out.println(col);
String b = new String("b");
String a2 = new String("a");
Collection col = new HashSet();
col.add(a);
col.add(b);
// col.add(a2);
a = "";
col.remove(a);
// col.remove(a2);
System.out.println(col);
System.out.println( col.remove(a));
//false
Result:
[b, a]
因为a已经被重新赋值,代表一个新的字符串。
因为 String a = new String("a");
String b = new String("b");
String a2 = new String("a");
Collection col = new HashSet();
col.add(a);
col.add(b);/*这个时候col对象中包含了两个String元素"a","b"*/
// col.add(a2);
a = ""; /*更改a引用类型指向的对象*/
col.remove(a);/*从col中移除a引用所指向的对象,因为col中不包含"",所以remove失败*/
// col.remove(a2);
System.out.println(col);/*结果[b,a]*/