public class CollectionTest3 {
public static void main(String[] args) {
TreeSet set = new TreeSet();
set.add(123);
set.add(456);
set.add(34);
set.add(100);
System.out.println(set);
}
}
[34, 100, 123, 456]
import java.util.Objects;
/**
* @author xianyu
* @version 1.0
* @date 2019/12/29 21:00
*/
public class Person implements Comparable{
private String name;
private Integer age;
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", 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 Objects.equals(name, person.name) &&
Objects.equals(age, person.age);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
// 自然排序,安装姓名从大到小排列,再按照年龄从小到大排序
@Override
public int compareTo(Object o) {
if(o instanceof Person){
Person p = (Person)o; // 强转
int compare = -this.name.compareTo(p.name);
if(compare != 0){
return compare;
} else {
return Integer.compare(this.age,p.age);
}
}else{
throw new RuntimeException("输入的类型不匹配");
}
}
}
import java.util.Iterator;
import java.util.TreeSet;
/**
* @author xianyu
* @version 1.0
* @date 2019/12/30 12:18
* TreeSet
*/
public class CollectionTest3 {
public static void main(String[] args) {
TreeSet set = new TreeSet();
set.add(new Person("Alice",23));
set.add(new Person("Mike",20));
set.add(new Person("Bob",18));
set.add(new Person("Tom",22));
set.add(new Person("Jerry",28));
set.add(new Person("Bob",24));
Iterator iterator = set.iterator();
while(iterator.hasNext()){ // hasNext():判断是否还有下一个元素
System.out.println(iterator.next()); // //next():①指针下移 ②将下移以后集合位置上的元素返回
}
}
}
Person{name='Tom', age=22}
Person{name='Mike', age=20}
Person{name='Jerry', age=28}
Person{name='Bob', age=18}
Person{name='Bob', age=24}
Person{name='Alice', age=23}
定制排序
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/**
* @author xianyu
* @version 1.0
* @date 2019/12/30 12:54
* 定制排序
*/
public class CollectionTest4 {
public static void main(String[] args) {
Comparator com = new Comparator() {
// 按照年龄从小到大排序
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Person && o2 instanceof Person){
Person p1 = (Person)o1;
Person p2 = (Person)o2;
return Integer.compare(p1.getAge(),p2.getAge());
}else{
throw new RuntimeException("输入的数据类型不匹配");
}
}
};
TreeSet set = new TreeSet(com);
set.add(new Person("Alice",23));
set.add(new Person("Jack",18));
set.add(new Person("Tom",22));
set.add(new Person("Jerry",28));
set.add(new Person("Bob",24));
Iterator iterator = set.iterator();
while(iterator.hasNext()){ // hasNext():判断是否还有下一个元素
System.out.println(iterator.next()); // //next():①指针下移 ②将下移以后集合位置上的元素返回
}
}
}
Person{name='Jack', age=18}
Person{name='Tom', age=22}
Person{name='Alice', age=23}
Person{name='Bob', age=24}
Person{name='Jerry', age=28}