TreeSet保证元素唯一并实现排序的原理

xiaoxiao2021-02-28  61

TreeSet:根据构造方法的不用,选择使用自然排序或者比较器排序。  按照实际的需求,可以对元素进行排序。并且保证唯一。怎么保证的呢?  排序:底层结构是二叉树。按照树节点进行存储和取出。  两种实现:  A:自然排序(元素具备比较性)  TreeSet的无参构造,要求对象所属的类实现Comparable接口。  B:比较器排序(集合具备比较性)  TreeSet的带参构造,要求构造方法接收一个实现了Comparator接口的对象。  唯一:根据返回值是否为0。  注意:  如果同时有两种方案,以谁为主呢?以比较器为主。  以比较器排序为主

方式一对存进集合的对象所属的类实现Comparable接口,进行自然排序

package cn.liuwen.treeset; /** * 需求:用TreeSet集合存储3个个Student对象, 然后遍历集合.(使用自然排序,按照元素添加顺序存储) * @author lwen * @version * @date 2015年7月7日 * 需求2:按照对象的姓名长度,从小到大排序,怎么做? */ 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 void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } //方式一,在要存入元素的那个对象类里实现Comparable接口 //重写CompareTo()方法 @Override public int compareTo(Student o) { Student s = o; // 将比较对象强转为Student类型 if (this.name.length() - s.name.length() > 0) { // 定义比较方式 return 1; } if (this.name.length() - s.name.length() == 0) { return this.name.compareTo(s.name);// 将比较结果返回 } return -1; } } //在主方法中传入上面的类对象 public class TreeSetDemo1 { public static void main(String[] args) { TreeSet<Student> ts = new TreeSet<Student>(); ts.add(new Student("liuwen",22)); ts.add(new Student("lifuwen",22)); ts.add(new Student("liuwen",22)); ts.add(new Student("guixin",12)); ts.add(new Student("guixiangd",18)); for (Student s:ts) { System.out.println(s.getName()+"****"+s.getAge()); } } }

方式二TreeSet的带参构造,要求构造方法接收一个实现了Comparator接口的对象。

public class Student2 { private String name; private int age; private int score; public Student2() { super(); // TODO Auto-generated constructor stub } public Student2(String name, int age, int score) { super(); this.name = name; this.age = age; this.score = score; } 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; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } //在主方法中传入这个类对象 public class TreeSetDemo3 { public static void main(String[] args) { //使用匿名内部类的方式,重写compare方法,在这个方法里对对象进行排序 TreeSet<Student2> ts = new TreeSet<Student2>(new Comparator<Student2>() { @Override public int compare(Student2 s1, Student2 s2) { int num = s2.getScore()-s1.getScore(); //定义排序方式 int num2 = (num==0)?(s1.getAge()-s2.getAge()):(num); int num3 = (num2==0)?(s1.getName().compareTo(s2.getName())):(num2); return num3; } }); ts.add(new Student2("liuwen",12,13)); ts.add(new Student2("liuwen",12,12)); ts.add(new Student2("liuwen",12,14)); ts.add(new Student2("lauwen",12,14)); ts.add(new Student2("liuwen",13,14)); ts.add(new Student2("liuwen",14,14)); ts.add(new Student2("lauwen",12,13)); for(Student2 s:ts){ System.out.println(s.getName()+"***"+s.getAge()+"***"+s.getScore()); } } }
转载请注明原文地址: https://www.6miu.com/read-65311.html

最新回复(0)