第30 章 : Set集合
130 Set接口简介
Set集合不允许保存重复数据
继承关系
-@Iterable
-@Collection
-@Set
-@SortedSet
-@NavigableSet
AbstractCollection(Collection)
-AbstractSet(Set)
-HashSet(Set, Cloneable, java.io.Serializable)
-TreeSet(NavigableSet, Cloneable, java.io.Serializable)
131 HashSet子类
HashSet元素无序
import java.util.HashSet;
import java.util.Set;
class Demo{
public static void main(String[] args) {
Set<String> list = new HashSet<String>();
list.add("Hello");
list.add("Hello");
list.add("World");
System.out.println(list);
// [Hello, World]
list.forEach(System.out::println);
/**
Hello
World
*/
}
}
132 TreeSet子类
TreeSet数据有序,按照升序排序
import java.util.Set;
import java.util.TreeSet;
class Demo{
public static void main(String[] args) {
Set<String> list = new TreeSet<String>();
list.add("Hello");
list.add("Hello");
list.add("World");
System.out.println(list);
// [Hello, World]
list.forEach(System.out::println);
/**
Hello
World
*/
}
}
133 分析TreeSet子类排序操作
TreeSet 通过TreeMap实现
需要实现Comparable接口
自定义类需要将所有属性进行比对,首选HashSet
import java.util.Set;
import java.util.TreeSet;
class Person implements Comparable<Person> {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person other) {
if (this.age > other.age) {
return 1;
} else if (this.age < other.age) {
return -1;
} else {
return this.name.compareTo(other.name);
}
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
class Demo {
public static void main(String[] args) {
Set<Person> list = new TreeSet<Person>();
list.add(new Person("Jack", 24));
list.add(new Person("Tom", 23));
list.add(new Person("Tom", 23));
System.out.println(list);
// [Person{name='Tom', age=23}, Person{name='Jack', age=24}]
list.forEach(System.out::println);
/**
Person{name='Tom', age=23}
Person{name='Jack', age=24}
*/
}
}
134 分析重复元素消除
实现重复元素判断
TreeSet Comparable接口
HashSet 对象编码 hashCode方法 ; 对象比较 equals方法
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
class Person implements Comparable<Person> {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public int compareTo(Person other) {
if (this.age > other.age) {
return 1;
} else if (this.age < other.age) {
return -1;
} else {
return this.name.compareTo(other.name);
}
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
class Demo {
public static void main(String[] args) {
Set<Person> list = new HashSet<Person>();
list.add(new Person("Jack", 24));
list.add(new Person("Tom", 23));
list.add(new Person("Tom", 23));
System.out.println(list);
// [Person{name='Tom', age=23}, Person{name='Jack', age=24}]
list.forEach(System.out::println);
/**
Person{name='Tom', age=23}
Person{name='Jack', age=24}
*/
}
}