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?
请大家教我!
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?
请大家教我!
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;
}
}