question_013-JAVA之Set之TreeSet
TreeSet
------------------------------------------------------
TreeSet【唯一性和排序性】
强制对加入的元素进行排序【排序的方法:自然排序和比较器排序】
·········默认是自然排序,对于【基础类型,会默认自然排序】
·········实现Comparable<T>接口的compare()方法,比较器排序【自定义类型一般要自己使用比较器排序】
-----------------------------------------------------
1)
使用元素的自然顺序对元素进行排序
2)
或者根据创建 set 时提供的 Comparator进行排序具体取决于使用的构造方法。
---------------------------------------------
对于自定义对象的排序方式1
package com.lyTs;
/**
* 如果一个类的元素要实现自然排序,必须实现自然排序接口
*
* @author Jack
*
*/
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 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 int compareTo(Student o) {
// return -1;
// 名称相同吗,不同则加入
// 名称相同,则比较name
int num = (this.age - o.age);
/**
* 如果一个类的元素要实现自然排序,必须实现自然排序接口
*
* @author Jack
*
*/
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 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 int compareTo(Student o) {
// return -1;
// 名称相同吗,不同则加入
// 名称相同,则比较name
int num = (this.age - o.age);
int num2 = num == 0 ? this.name.compareTo(o.name): num;
return num2;
}
}
return num2;
}
}
·············································
package com.lyTs;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>();
Student s1 = new Student("zhangsan",18);
Student s2 = new Student("lisi",21);
Student s3 = new Student("wangwu",33);
Student s4 = new Student("zhaoliu",45);
Student s5 = new Student("fengqi",6);
Student s6 = new Student("wangba.",33);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
for(Student i : ts){
System.out.println(i.getName()+"----"+ i.getAge());
}
}
}
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>();
Student s1 = new Student("zhangsan",18);
Student s2 = new Student("lisi",21);
Student s3 = new Student("wangwu",33);
Student s4 = new Student("zhaoliu",45);
Student s5 = new Student("fengqi",6);
Student s6 = new Student("wangba.",33);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
for(Student i : ts){
System.out.println(i.getName()+"----"+ i.getAge());
}
}
}
·····················结果···················
fengqi----6
zhangsan----18
lisi----21
wangba.----33
wangwu----33
zhaoliu----45
zhangsan----18
lisi----21
wangba.----33
wangwu----33
zhaoliu----45
················································
-----------------------------------------------------
对于自定义对象的排序方式2
·······
学生类中没有比较方法···············
package com.lyTsComparator;
/**
* @author Jack
*/
public class Student{
private String name;
private int age;
public Student() {
}
public Student(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;
}
}
/**
* @author Jack
*/
public class Student{
private String name;
private int age;
public Student() {
}
public Student(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;
}
}
···············比较器··················
package com.lyTsComparator;
import java.util.Comparator;
public class MyComparator implements Comparator< Student> {
@ Override
public int compare(Student o1, Student o2) {
// 姓名的长度相同
int num = o1.getName().length() - o2.getName().length();
// 姓名的长度相同,不代表内容相同
int num2 = num == 0? o1.getName().compareTo(o2.getName()):num;
int num3 = num2 == 0?o1.getAge() - o2.getAge():num2;
return num3;
}
}
import java.util.Comparator;
public class MyComparator implements Comparator< Student> {
@ Override
public int compare(Student o1, Student o2) {
// 姓名的长度相同
int num = o1.getName().length() - o2.getName().length();
// 姓名的长度相同,不代表内容相同
int num2 = num == 0? o1.getName().compareTo(o2.getName()):num;
int num3 = num2 == 0?o1.getAge() - o2.getAge():num2;
return num3;
}
}
···············
测试··
················
package com.lyTsComparator;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
Student s1 = new Student("zhangsan",18);
Student s2 = new Student("lisi",21);
Student s3 = new Student("wangwu",33);
Student s4 = new Student("zhaoliu",45);
Student s5 = new Student("fengqi",6);
Student s7 = new Student("fengqi",33);
Student s6 = new Student("wangba.",33);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
for(Student i : ts){
System.out.println(i.getName()+"----"+ i.getAge());
}
}
}
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
Student s1 = new Student("zhangsan",18);
Student s2 = new Student("lisi",21);
Student s3 = new Student("wangwu",33);
Student s4 = new Student("zhaoliu",45);
Student s5 = new Student("fengqi",6);
Student s7 = new Student("fengqi",33);
Student s6 = new Student("wangba.",33);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
for(Student i : ts){
System.out.println(i.getName()+"----"+ i.getAge());
}
}
}
```````````````j结果```````````````
lisi----21
fengqi----6
wangwu----33
wangba.----33
zhaoliu----45
zhangsan----18
fengqi----6
wangwu----33
wangba.----33
zhaoliu----45
zhangsan----18
-----------------------------------------------------
对于自定义对象的排序方式3
·······
学生类中没有比较方法···············
package com.lyTsComparator;
/**
* @author Jack
*/
public class Student{
private String name;
private int age;
public Student() {
}
public Student(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;
}
}
/**
* @author Jack
*/
public class Student{
private String name;
private int age;
public Student() {
}
public Student(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;
}
}
···············
测试··
················
使用了匿名比较器
package com.lyTsComparator;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
// 使用匿名内部类,来实现
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// 姓名的长度相同
int num = o1.getName().length() - o2.getName().length();
// 姓名的长度相同,不代表内容相同
int num2 = num == 0? o1.getName().compareTo(o2.getName()):num;
int num3 = num2 == 0?o1.getAge() - o2.getAge():num2;
return num3;
}
});
Student s1 = new Student("zhangsan",18);
Student s2 = new Student("lisi",21);
Student s3 = new Student("wangwu",33);
Student s4 = new Student("zhaoliu",45);
Student s5 = new Student("fengqi",6);
Student s7 = new Student("fengqi",33);
Student s6 = new Student("wangba.",33);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
for(Student i : ts){
System.out.println(i.getName()+"----"+ i.getAge());
}
}
}
```````````````j结果```````````````
lisi----21
fengqi----6
wangwu----33
wangba.----33
zhaoliu----45
zhangsan----18
fengqi----6
wangwu----33
wangba.----33
zhaoliu----45
zhangsan----18
··························································