import java.util.*;public class HashSet {
public static void main(String[] args) {
Collection c = new HashSet();
c.add("hello");
c.add(new Name("f1","l1"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c.remove(new Name("f1","l1")));
System.out.println(c);
}
}class Name {
private String firstName,lastName;

Name(String firstName,String lastName){
this.firstName = firstName;
this.lastName = lastName;
}

  public String getFirstName() {
return firstName;
  } public String getLastName() {
return lastName;
} public String toString(){
return firstName + " " + lastName;
}
}我看的是马士兵老师的教程,他演示的时候可以通过,为什么我做时就提示:
类型不匹配:不能从hashset转换为collection?
请大家教我!

解决方案 »

  1.   

    hashset不是collection接口下的类,而map下的
      

  2.   

    请问:是不是跟jdk版本有关呢?他用的是jdk5.0
      

  3.   

    没有问题啊,你这个class换个名字 好不,不要叫hashset,以免混淆import java.util.Collection;
    import java.util.HashSet;public class HashSetTest
    {
        public static void main(String[] args)
        {
            Collection c = new HashSet();
            c.add("hello");
            c.add(new Name("f1", "l1"));
            c.add(new Integer(100));
            c.remove("hello");
            c.remove(new Integer(100));
            System.out.println(c.remove(new Name("f1", "l1")));
            System.out.println(c);
        }
    }class Name
    {
        private String firstName, lastName;    Name(String firstName, String lastName)
        {
            this.firstName = firstName;
            this.lastName = lastName;
        }    public String getFirstName()
        {
            return firstName;
        }    public String getLastName()
        {
            return lastName;
        }    public String toString()
        {
            return firstName + " " + lastName;
        }
    }