自然排序
TreeSet集合存储自定义对象(Student)
按照姓名的长度从小到大进行比较 :主要条件
public class TreeSetDemo {
public static void main(String[] args) {
//创建集合对象
TreeSet <Student> set = new TreeSet<Student>();
//创建学生对象
Student s1 = new Student("yangyubo",23 );
Student s2 = new Student("zhangchen",22 );
Student s3 = new Student("yangyubo",23 );
Student s4 = new Student("yutao",34 );
Student s5 = new Student("yutao",22 );
Student s6 = new Student("guanli",22 );
Student s7 = new Student("guanli",22 );
//添加Student对象到集合中
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
set.add(s6);
set.add(s7);
//遍历集合
for(Student stu : set) {
System.out.println(stu.getName()+"---"+stu.getAge());
}
}
}
创建Student类
Student类实现Comparable接口,并且重写了compareTo ()方法,是字典比较方法
public class Student implements Comparable<Student> {
private String name ;
private int age ;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
/*TreeSet集合存储自定义对象(Student)
*
* 按照姓名的长度从小到大进行比较 :主要条件*/
@Override //compareTo 是字典比较方法
public int compareTo(Student s) {
// TODO Auto-generated method stub
//比较姓名的长度
int num = this.getName().length()-s.getName().length();
//比较名字内容
int num1 = this.getName().compareTo(s.getName());
//比较年龄
int num2 =num1 =num==0? this.getAge()-s.getAge():num;
return num1;
}
}
选择排序
分为两种:
1.直接使用匿名内部类的方式实现
2.自定义一个类并且实现Comparator接口,并且重写Compare()方法
先展示第一种匿名内部类的方式
public class TreeSetDemo1 {
public static void main(String[] args) {
//创建集合对象
//直接使用匿名内部类的方式实现
TreeSet<Student> set = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
/*TreeSet集合存储自定义对象(Student)
*
* 按照姓名的长度从小到大进行比较 :主要条件*/
//比较名字长度
int num1 = s1.getName().length()-s2.getName().length();
//比较名字内容
int num2 = s1.getName().compareToIgnoreCase(s2.getName());
//比较年龄大小
int num3 = num2 ==num1? s1.getAge()-s2.getAge():num2;
return num3;
}
});
//创建学生对象
Student s1 = new Student("yangyubo",23 );
Student s2 = new Student("zhangchen",22 );
Student s3 = new Student("yangyubo",23 );
Student s4 = new Student("yutao",34 );
Student s5 = new Student("yutao",22 );
Student s6 = new Student("guanli",22 );
Student s7 = new Student("guanli",22 );
//添加Student对象到集合中
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
set.add(s6);
set.add(s7);
//遍历集合
for(Student stu : set) {
System.out.println(stu.getName()+"---"+stu.getAge());
}
}
}
创建正常的学生类
public class Student {
private String name ;
private int age ;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}
展示第二种自定义类的选择排序方法
创建自定义类YB并且实现Comparator接口,重写compare()方法
要注意导包!!!
public class yb implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
/*TreeSet集合存储自定义对象(Student)
*
* 按照姓名的长度从小到大进行比较 :主要条件*/
//比较名字长度
int num1 = s1.getName().length()-s2.getName().length();
//比较名字内容
int num2 = s1.getName().compareToIgnoreCase(s2.getName());
//比较年龄大小
int num3 = num2 ==num1? s1.getAge()-s2.getAge():num2;
return num3;
}
}
创建TreeSet集合测试类
public class TreeSetDemo {
public static void main(String[] args) {
//创建集合对象
//Comparator是一个接口
TreeSet<Student> set = new TreeSet<Student>(new yb()) ;
//创建学生对象
Student s1 = new Student("yangyubo",23 );
Student s2 = new Student("zhangchen",22 );
Student s3 = new Student("yangyubo",23 );
Student s4 = new Student("yutao",34 );
Student s5 = new Student("yutao",22 );
Student s6 = new Student("guanli",22 );
Student s7 = new Student("guanli",22 );
//添加Student对象到集合中
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
set.add(s6);
set.add(s7);
//遍历集合
for(Student stu : set) {
System.out.println(stu.getName()+"---"+stu.getAge());
}
}
}
创建一个普通的学生类
public class Student {
private String name ;
private int age ;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}