Set集合及其子类:
1. Set
A:Set集合的特点无序性(即存储和取出不一致),集合中的元素不可以重复
面试题:
List集合和Set集合有什么区别?List集合:存储的元素是可以重复的,并且存储的元素和取出的一致,保证有序性
Set集合:存储的元素是不可以重复的,并且存储的元素和取出的不一致,无序。
2. HashSet
A:HashSet实现Set接口,它的底层数据结构是哈希表,它不保证Set的迭代顺序,该类允许使用null元素B:HashSet保证元素的唯一性
C:哈希表底层依赖两个方法:
HashCode(),equals()
如果HashCode()码值一样,还要依赖equals()方法比较内容,true:元素重复了,不添加;false,直接把元素添加到集合中。
集合存储String类型,本身就重写了HashCode()方法和equals()方法
D:HashSet存储字符串并遍历:
public class HashSetDemo2 {
public static void main(String[] args) {
//创建HashSet集合对象
HashSet<Student> hs = new HashSet<Student>() ;
//创建6个学生对象
Student s1 = new Student("高圆圆", 27) ;
Student s2 = new Student("唐嫣", 25) ;
Student s3 = new Student("黄晓明", 25) ;
Student s4 = new Student("高圆圆", 27) ;
Student s5 = new Student("邓超", 29) ;
Student s6 = new Student("邓超", 29) ;
System.out.println(s1);
System.out.println(s4);
//添加元素
hs.add(s1) ;
hs.add(s2) ;
hs.add(s3) ;
hs.add(s4) ;
hs.add(s5) ;
hs.add(s6) ;
//遍历
for(Student s: hs){
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
E:HashSet存储自定义对象
//Student
public class Student {
private String name;
private int age ;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
//测试类
public class HashSetDemo2 {
public static void main(String[] args) {
//创建HashSet集合对象
HashSet<Student> hs = new HashSet<Student>() ;
//创建6个学生对象
Student s1 = new Student("高圆圆", 27) ;
Student s2 = new Student("唐嫣", 25) ;
Student s3 = new Student("黄晓明", 25) ;
Student s4 = new Student("高圆圆", 27) ;
Student s5 = new Student("邓超", 29) ;
Student s6 = new Student("邓超", 29) ;
System.out.println(s1);
System.out.println(s4);
//添加元素
hs.add(s1) ;
hs.add(s2) ;
hs.add(s3) ;
hs.add(s4) ;
hs.add(s5) ;
hs.add(s6) ;
//遍历
for(Student s: hs){
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
3. LinkedSet
A:LinkedSet底层数据结构是由哈希表和链表实现哈希表:保证元素的唯一性
链表:保证元素的有序性
4.TreeSet
A:TreeSet集合保证元素的唯一性:并且还可以排序:基于TreeMap集合实现,又属于Map<K,V>集合的子类:双列集合了
而TreeMap集合:底层是根据红黑树结构实现的
B:两种排序:
自然排序
比较强排序
要使用哪种排序,是根据你的构造方法进行实现:
TreeSet():无参构造方法:走的自然排序
public class TreeSetDemo {
public static void main(String[] args) {
//创建TreeSet集合对象
TreeSet<Integer> ts = new TreeSet<Integer>() ;
//给集合中添加元素
//20,17,22,23,24,19,18,24
ts.add(20) ;
ts.add(17) ;
ts.add(22) ;
ts.add(23) ;
ts.add(24) ;
ts.add(19) ;
ts.add(18) ;
ts.add(24) ;
//遍历TreeSet集合
for(Integer i : ts){
System.out.print(i+" ");
}
}
}