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呢?

解决方案 »

  1.   

    new Name
    你这是新对象
    如果你需要让 属性相同的 对象在 collection 中算同一个对象的话
    去重写 Name 的 hashcode 和 equels 方法
      

  2.   

    boolean remove(Object o)从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。更确切地讲,如果此 collection 包含一个或多个满足 (o==null ? e==null : o.equals(e)) 的元素 e,则移除这样的元素。如果此 collection 包含指定的元素(或者此 collection 由于调用而发生更改),则返回 true 。 参数:
    o - 要从此 collection 中移除的元素(如果存在)。 
    返回:
    如果此调用将移除一个元素,则返回 true 
    看完这个 LZ应该明白一切了吧!
      

  3.   


    就是看了这个还是不明白。按照这个说法o==null不满足执行o.equals(e))    这个e指的事什么?
    这个应该是相等的啊!是ture   问什么是false呢?
    remove方法的实现过程是什么样的,它调用equals和hashcode方法,我没重写。这个是后它调用的是哪的equals和hashcode方法呢?
      

  4.   

    e 指的是collection中的每一个元素;
    remove方法的实现过程是什么样的-- 这你看HashSet的源码就好了。
    我没重写。这个是后它调用的是哪的equals和hashcode方法呢? --- 调用Object中默认的equals和hashcode
      

  5.   

    这个要重写equels和hashCode方法,因为判断一个对象是否相同时根据hash值的
    你new name就是新对象了
      

  6.   

    你new了一个新的Name,remove删除的是你new的这个新Name.这个新Name不在collection当中。如楼上各位所说,你重写Name的equals和hashcode,让他根据内容判断是否相等就行了
      

  7.   

    现在改成这样
    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]     没有删去吗?
      

  8.   

    现在结果是true了为什么c中的n还在呢?
      

  9.   

    c.add(new Name("f1","11"));
    这个没删不是很正常的吗
    你只是删除了 n
    你的 n 虽然 和后来 new Name("f1","11") 的对象属性一样
    但他们不是同一个对象
      

  10.   

    Name n = new Name("f1","11");//声明一个n
    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
      

  11.   

    n = new Name("f1","11");
    c.add(new Name("f1","11"));
    c.add(n);
    你不觉得应该是加入了2个有相同属性的 Name 对象吗
    然后
    c.remove(n);
    这个只是删了 n 而已
      

  12.   

    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]
    true
    []这样就对了,    
    谢谢大家的帮助!
      

  13.   

    嗯,楼上这样也可以顺便查了一下api重写toString其实是重写了hashcode