/**
* created by yangyinglong at 20180517
* 排序方式的学习
* Comparator 和 Comparable 两个接口的学习
*/
import java.util.*;
public class ComparatorTest {
public static void main(String[] argv) {
List<Person> personList = new ArrayList<Person>();
personList.add(new Person(11, "xiaoming", 1));
personList.add(new Person(12, "xiaoming", 1));
personList.add(new Person(12, "xiaoming", 0));
personList.add(new Person(12, "xiaohong", 1));
personList.add(new Person(13, "xiaohong", 0));
personList.add(new Person(11, "xiaoming", 0));
personList.add(new Person(13, "xiaoming", 1));
//对personList随机排列
Collections.shuffle(personList);
Iterator<Person> iterator_1 = personList.iterator();
System.out.println("随机排序之后的结果");
System.out.println(" NAME \tAGE\tSEX");
while (iterator_1.hasNext()) {
Person per = iterator_1.next();
System.out.println(per);
}
// 随机排序之后的结果
// NAME AGE SEX
// xiaoming 11 0
// xiaoming 11 1
// xiaoming 13 1
// xiaoming 12 1
// xiaohong 13 0
// xiaohong 12 1
// xiaoming 12 0
//对personList用PerComparator类进行排序
Collections.sort(personList, new PerComparator());
System.out.println("用PerComparator排序之后的结果");
Iterator<Person> iterPer_2 = personList.iterator();
while (iterPer_2.hasNext()) {
Person per = iterPer_2.next();
System.out.println(per);
}
// 用PerComparator排序之后的结果
// xiaohong 12 1
// xiaohong 13 0
// xiaoming 11 0
// xiaoming 11 1
// xiaoming 12 0
// xiaoming 12 1
// xiaoming 13 1
// // test2
// ArrayList<Student> arrayList = new ArrayList<Student>();
// arrayList.add(new Student("zhangsan",89.8));
// arrayList.add(new Student("lisi",90));
// arrayList.add(new Student("wangwu",60.6));
// arrayList.add(new Student("wangting",85.6));
// Iterator<Student> iterator_stu = arrayList.iterator();
// while (iterator_stu.hasNext()) {
// Student stu_one = iterator_stu.next();
// System.out.println(stu_one);
// }
// // zhangsan 89.8
// // lisi 90.0
// // wangwu 60.6
// // wangting 85.6
// // System.out.println(arrayList);
// // ×××××××××排序×××××××××××
// Collections.sort(arrayList);
// // for (Student stu: arrayList) {
// // System.out.println(stu);
// // }
// Iterator<Student> iterator_stu_1 = arrayList.iterator();
// while (iterator_stu_1.hasNext()) {
// Student stu_one = iterator_stu_1.next();
// System.out.println(stu_one);
// }
// // lisi 90.0
// // zhangsan 89.8
// // wangting 85.6
// // wangwu 60.6
}
}
class Person {
private int age;
private String name;
//1代表男,0代表女
private int sex;
public Person(int age, String name, int sex) {
this.age = age;
this.name = name;
this.sex = sex;
}
public int getAge() {
return this.age;
}
public String getName() {
return this.name;
}
public int getSex() {
return this.sex;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setSex(int sex) {
this.sex = sex;
}
public String toString() {
return this.name + "\t" + this.age + "\t" + this.sex;
}
}
class PerComparator implements Comparator<Person> {
@Override
public int compare(Person person1, Person person2) {
int cr = 0;
//按姓名升序排序
int a = person1.getName().compareTo(person2.getName());
if (a != 0) {
cr = (a > 0) ? 3 : -1;
} else {
//按年龄升序排序
a = person1.getAge() - person2.getAge();
if (a != 0) {
cr = (a > 0) ? 2 : -2;
} else {
a = person1.getSex() - person2.getSex();
if (a != 0) {
cr = (a > 0) ? 1 : -3;
}
}
}
return cr;
}
}
// Comparable接口中的compareTo()方法:
// 凡是需要进行比较排序的类都应该实现Comparable接口中的compareTo()方法;
// 凡是把类对象放到以树为内部结构的容器中都应该实现Comparable接口中的compareTo()方法 test3
// 1、凡是把类对象放到容器中,相应的类都应该实现Object类中的toString()方法;
// 2、凡是需要进行比较排序的类都应该实现Comparable接口中的compareTo()方法;
// 凡是把类对象放到以树为内部结构的容器中都应该实现Comparable接口中的compareTo()方法
// 3、凡是把类对象放到以哈希表为内部存储结构的容器中,相应的类必须要实现equals方法和hashCode方法,
// 这样才符合哈希表真实的逻辑功能.
// 4、逻辑上来讲,只要两个对象的内容相同,其地址(hashCode()返回值)以及这两个对象就应该相同(equals())。
public class Student implements Comparable<Object> {
private String name;
private double score;
public Student(String name, double score) {
this.name = name;
this.score = score;
}
public String getName() {
return this.name;
}
public double getScore() {
return this.score;
}
public String toString() {
return this.name + "\t" + this.score;
}
@Override
public int compareTo(Object s) {
Student stu = (Student)s;
if(stu.getScore() > this.score) {
return 1;
} else if (stu.getScore() < this.score) {
return -1;
} else {
return 0;
}
}
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student stu = (Student)obj;
return this.getName().equals(stu.getName());
}
return super.equals(obj);
}
public int hashCode() {
return (int)(this.getName().hashCode()*this.getScore());
}
}