HashSet
HashSet方法可以保证元素的唯一性,但是前提是需要重写equals和hashcode方法。由于HashSet底层是数据结构是哈希表,而哈希表可以保证元素的唯一。
HashSet<Student> stu = new HashSet<>();
stu.add(new Student("熊大" ,18 ));
stu.add(new Student("熊二" ,19 ));
stu.add(new Student("光头强" ,19 ));
stu.add(new Student("熊大" ,18 ));
for (Student s:stu){
System.out.println(s);
}
}
public class Student {
private String name;
private int age;
public Student () {
}
public Student (String name, int age) {
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 boolean equals (Object o) {
if (this == o) return true ;
if (o == null || getClass() != o.getClass()) return false ;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode () {
return Objects.hash(name, age);
}
@Override
public String toString () {
return "Student{" +
"name=" + name +
", age=" + age +
'}' ;
}
}
LinkedHashSet
LinkedHashSet :元素有序(元素存取顺序)且唯一 ,底层数据结构是链表和哈希表。链表保证元素有序,哈希表保证元素唯一。
public class MyTest {
public static void main(String[] args) {
LinkedHashSet<Integer> list = new LinkedHashSet<>();
list .add(Integer.valueOf(1 ));
list .add(4 );
list .add(2 );
list .add(5 );
list .add(3 );
list .add(5 );
list .add(1 );
list .add(2 );
System.out.println(list );
}
}
TreeSet
public class MyTest {
public static void main (String[] args) {
TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.add(6 );
treeSet.add(6 );
treeSet.add(2 );
treeSet.add(1 );
treeSet.add(7 );
treeSet.add(9 );
treeSet.add(10 );
treeSet.add(8 );
treeSet.add(5 );
treeSet.add(4 );
treeSet.add(3 );
for (Integer num:treeSet){
System.out .print(num+" " );
}
}
}
TreeSet排序的方式有两种 (1)自然排序 TreeSet 空参构造采用的就是自然排序 (2)比较器排序 TreeSet 有参构造采用的是 比较器排序 自然排序: 之所以TreeSet 采用自然排序,能够对元素进行排序是因为元素实现了Compareble接口,并且重写了compareTo(T o) 这个方法实现的排序。如果元素不重写此接口中的方法,那么在存元素的时候,就会报错,而我们的String 和 Integer 都默认实现了Compareble接口并重写了compareTo(T o) 这个方法。
Student类
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student () {
}
public Student (String name, int age) {
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 String toString () {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}' ;
}
@Override
public int compareTo (Student o) {
return -(this .age==o.age?this .name.compareTo(o.name):this .age-o.age);
}
}
Test类
public class MyTest2 {
public static void main (String[] args) {
TreeSet<Student> t = new TreeSet<>();
t.add(new Student("张三" ,23 ));
t.add(new Student("李四" ,24 ));
t.add(new Student("小明" ,24 ));
t.add(new Student("张三" ,23 ));
t.add(new Student("王武" ,25 ));
t.add(new Student("田七" ,27 ));
t.add(new Student("赵六" ,26 ));
for (Student stu:t){
System.out .println(stu);
}