Set接口下的集合
<>范型
1.HashSet
HashSet set = new HashSet();
set.add("aa");
set.add(111);
set.add("bb");
set.add("aa");
set.add("cc");
System.out.println(set.size());
//因为是无序的,所以不能用for循环读取,而是用增强for循环(foreach循环:快捷键alt + /)或者迭代器Iterator
for(Object obj : set) {
System.out.println(obj);
}
Iterator it = set.iterator();
while(it.hasNext()) {
Object obj = it.next();
System.out.println(obj);
}
集合中也可以添加其它的类,但是Set下的集合是无序的,读取时不方便,所以必须使用包装类
public class Student {
private int id;
private String name;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Student() {
}
public Student(int id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}