TreeSet保证元素唯一并实现排序的原理
2017-08-06 22:35
411 查看
TreeSet:根据构造方法的不用,选择使用自然排序或者比较器排序。
按照实际的需求,可以对元素进行排序。并且保证唯一。
怎么保证的呢?
排序:底层结构是二叉树。按照树节点进行存储和取出。
两种实现:
A:自然排序(元素具备比较性)
TreeSet的无参构造,要求对象所属的类实现Comparable接口。
B:比较器排序(集合具备比较性)
TreeSet的带参构造,要求构造方法接收一个实现了Comparator接口的对象。
唯一:根据返回值是否为0。
注意:
如果同时有两种方案,以谁为主呢?以比较器为主。
以比较器排序为主
按照实际的需求,可以对元素进行排序。并且保证唯一。
怎么保证的呢?
排序:底层结构是二叉树。按照树节点进行存储和取出。
两种实现:
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()); } } }
相关文章推荐
- TreeSet保证元素唯一并实现排序的原理
- 集合框架_TreeSet保证元素唯一性和比较器排序的原理及代码实现
- Java_基础—TreeSet保证元素唯一和自然排序
- 集合框架-TreeSet保证元素唯一性和自然排序的原理和图解
- 集合框架_TreeSet保证元素唯一性和自然排序的原理和图解(二叉树结构)
- Java基础知识强化之集合框架笔记47:Set集合之TreeSet保证元素唯一性和比较器排序的原理及代码实现(比较器排序)
- TreeSet保证元素的 排序和唯一性 的原理
- Treeset保证元素唯一且排序的代码示例
- TreeSet对元素进行排序实现的两种方式
- 编写一个多线程函数实现对数组排序,要求: 1.至少用两个线程 2.数组的元素值可以事先定义好,或者可以从键盘输入(增加一个线程)。 3.用一个线程对数组排序,用另一个线程输出排序结果。 4.保证先排好序,再输出。
- 集合的特殊功能之TreeSet集合如何保证元素的唯一及排序的
- TreeSet存储自定义对象并保证排序和唯一
- Java基础知识强化之集合框架笔记44:Set集合之TreeSet保证元素唯一性和自然排序的原理和图解
- TreeSet存储元素自然排序和唯一的图解
- TreeSet保证元素唯一性原理图解
- HashSet类是如何实现添加元素保证不重复的---哈希码的原理
- 集合框架_TreeSet保证元素排序的源码解析
- C语言 选择排序算法原理和实现 从数组中 找出最小的元素然后交换位置
- TreeSet存储元素自然排序和唯一的代码及图解
- Python实现的选择排序算法原理与用法实例分析