# Java集合之TreeSet **Repository Path**: fpfgitmy_admin/java-collection-treeset ## Basic Information - **Project Name**: Java集合之TreeSet - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-04-28 - **Last Updated**: 2021-04-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #### TreeSet源码分析 + treeSet底层用的红黑树 + 可以按照对象的属性值进行排序 + 向TreeSet中添加的数据,要求是相同类的对象 + 两种排序方式:自然排序(实现Comparable接口)和定制排序(Comparator) + 自然排序中,比较两个对象是否相同的标准为:compareTo()返回0,不再是equals() + 定制排序中,比较两个对象师傅相同的标准为:compare()返回0,不再是equals() ##### 自然排序模拟 1. 创建实现Comparable接口的类并重写compareTo()方法 ``` package com.felixfei.study.model; /** * @describle */ public class User implements Comparable { private String name; private int age; public User(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 "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } // 按照姓名从小到大排序,按照年龄从小到大排序 @Override public int compareTo(Object o) { if (o instanceof User) { User user = (User) o; int compare = this.name.compareTo(user.name); if (compare != 0) { return compare; } else { return Integer.compare(this.age, user.age); } } else { throw new RuntimeException("输入类型不匹配"); } } } ``` 2. TreeSet方法的使用 ``` package com.felixfei.study.test; import com.felixfei.study.model.User; import java.util.Iterator; import java.util.Set; import java.util.TreeSet; /** * @describle */ public class TestTreeSet { public static void main(String[] args) { TreeSet set = new TreeSet<>(); set.add(new User("Tom",12)); set.add(new User("Jack",11)); set.add(new User("Mike",22)); set.add(new User("Jim",23)); set.add(new User("Jim",56)); Iterator iterator = set.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } } ``` ##### 定制排序模拟 1. 编写user类 ``` package com.felixfei.study.model; /** * @describle */ public class User{ private String name; private int age; public User(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 "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } } ``` 2. 定制排序模拟 ``` package com.felixfei.study.test; import com.felixfei.study.model.User; import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; /** * @describle */ public class TestTreeSet { public static void main(String[] args) { // 编写定制排序方法 Comparator com = new Comparator() { // 按照年龄从小到大排列 @Override public int compare(Object o1, Object o2) { if (o1 instanceof User && o2 instanceof User) { User u1 = (User) o1; User u2 = (User) o2; return Integer.compare(u1.getAge(), u2.getAge()); } else { throw new RuntimeException("输入类型不匹配"); } } }; TreeSet set = new TreeSet<>(com); set.add(new User("Tom", 12)); set.add(new User("Jack", 11)); set.add(new User("Mike", 22)); set.add(new User("Jim", 23)); set.add(new User("Jim", 56)); Iterator iterator = set.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } } ```