Set courseSet = new HashSet();
Set<String> courseSet1 = new HashSet<String>();
Set<String> h = new HashSet<String>(Arrays.asList("a", "b"));
List和Set集合是可以像String类型那样声明和初始化的,区别在于Set本身是一个接口,只能实例化它的实现类,比如上面贴的HashSet。像第三行那样声明并初始化使用也可以。
public class Student{
public String id;
public String name;
public Set courses;
public Student(String id, String name){
this.id = id;
this.name = name;
this.courses = new HashSet();
}
}