目录:
一:特点
二:遍历方式
三:常用实现类
一:特点
无序,对象不能重复(唯一)。
唯一:
1:不允许添加重复元素
2:不重写equals方法,默认比较的是Object类型,而Object是引用类型,比较是内存地址
3:重写equals+hashcode,先比较hashcode值,再比较equals
3.1:hashcode值相同,则比较equals
3.2:hashcode值不相同,则不比较equals
二:遍历方式
2.1:foreach
Set<Student> set=new HashSet<>();
for (Student student : set) {
System.out.println(student);
}//student为自定义实体类
2.2:迭代器
Set<Student> set=new HashSet<>();
//获取迭代器
Iterator<Student> it=set.iterator();
//便利循环
while(it.hasNext()) {
System.out.println(it.next());
}
三:常用实现类