TreeSet:
Treeset是一个有序的集合,它的作用是提供有序的Set集合,可以按照添加对象的属性进行排序。
注意: 向TreeSet中添加数据时,要求数据时相同类的对象。
自然排序(实现Comparable接口):自然排序中,比较两个对象相同的方法时compareTo,如果一样返回0。
整数类型:
package com.cheng.collection;
import org.junit.Test;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetTest {
@Test
public void test01(){
Set set = new TreeSet();
//add的元素需要是同一类型
set.add(123);
set.add(99);
set.add(13);
set.add(23);
set.add(3);
set.add(-125);
Iterator iterator = set.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + "\t");
}
//输出结果:
//-125 3 13 23 99 123
}
}
String类型:
package com.cheng.collection;
import org.junit.Test;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetTest {
@Test
public void test01(){
Set set = new TreeSet();
//add的元素需要是同一类型
set.add("sda");
set.add("da");
set.add("azs");
set.add("out");
set.add("go");
Iterator iterator = set.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + "\t");
}
//输出结果:
//azs da go out sda
}
}
Person类:
@Test
public void test03(){
Set set03 = new TreeSet();
set03.add(new Person("Tom",22));
set03.add(new Person("Jerry",36));
set03.add(new Person("Mako",16));
set03.add(new Person("Lily",28));
set03.add(new Person("Bram",89));
Iterator iterator = set03.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
Person类重写compareTo方法:
package com.cheng.collection;
import java.util.Objects;
public class Person implements Comparable{//此处实现Comparable接口
private String name;
private int age;
public Person() {
}
public Person(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 "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//重写equals如下
@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 compareTo(Object o) {
if (o instanceof Person){
Person person = (Person)o;
return this.name.compareTo(person.name);
//return -this.name.compareTo(person.name); 降序用这个 this前面就加了个负号
}else{
throw new RuntimeException("输入类型不匹配");
}
}
}
//按照姓名升序排序,此时有两个姓名一样但年龄不同的person
@Override
public int compareTo(Object o) {
if (o instanceof Person){
Person person = (Person)o;
//return this.name.compareTo(person.name);
int compare = this.name.compareTo(person.name);
if (compare != 0){//两人的名字不同
return compare;//返回compare
}else {//名字一样
return Integer.compare(this.age,person.age);
//比较年龄
//按照年龄升序排列
}
}else{
throw new RuntimeException("输入类型不匹配");
}
}
定制排序:
@Test
public void test04(){
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("输入类型异常");
}
}
};
Set set04 = new TreeSet(com);
set04.add(new Person("Tom",22));
set04.add(new Person("Jerry",36));
set04.add(new Person("Mako",16));
set04.add(new Person("Lily",28));
set04.add(new Person("Bram",89));
Iterator iterator = set04.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}