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);

解决方案 »

  1.   


    System.out.println( col.remove(a));
    //false
      

  2.   

    应该是:
    Result:
    [b, a]
      

  3.   

    [b, a]
    因为a已经被重新赋值,代表一个新的字符串。
      

  4.   

    【b,a]或者是[a,b]
    因为        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]*/
      

  5.   

    每一行代码后面,都加上System.out.println(col);
      

  6.   

    hashset不按照输入顺序排序吧 我觉得有可能是a b