import java.util.Random;import java.util.*;import java.util.Comparator; import java.io.*; public class StuCompExample{ public static void main(String[] args)throws IOException{ List stus = new ArrayList(); for(int i = 0 ; i < 15 ; i++){ Student stu = new Student(generateStuName(),generateStuAge()); stus.add(stu); } System.out.println(" 该集合刚刚被创建 !"); printCollection(stus); System.out.println("该集合将要被排序 !"); try{ Collections.sort(stus,new StudentAgeComparator()); }catch(Exception e){ //System.out.println(e); new PrintStream("aaa.txt").println(e); e.printStackTrace(new PrintStream("bbb.txt")); } //Collections.sort(stus); printCollection(stus); System.out.println("下面要反序了 "); Collections.reverse(stus); printCollection(stus); System.out.println("下面要打乱顺序了 "); Collections.shuffle(stus); printCollection(stus); System.out.println("下面是最大值"); System.out.println( Collections.max(stus,new StudentAgeComparator())); System.out.println("下面是最小值"); System.out.println(Collections.min(stus,new StudentAgeComparator())); } public static String generateStuName(){ char[] nc = new char[8]; for(int i = 0 ; i < nc.length ; i ++){ Random rand = new Random(); int j = 0 ; if(rand.nextBoolean()){ j = rand.nextInt( (char)'Z'-(char)'A')+(char)'A'; }else{ j = rand.nextInt( (char)'z'-(char)'a')+(char)'a'; } nc[i] = (char)j; } return new String(nc); } public static int generateStuAge(){ Random rand = new Random(); return rand.nextInt(10)+15 ; } public static void printCollection(Collection c){ System.out.println("------------------------------"); Iterator it = c.iterator(); while(it.hasNext()){ System.out.println(it.next()); } System.out.println("###############################"); System.out.println(); }}
class Student implements Comparable { int age ; String name ; Student(String name , int age ){ this.age = age ; this.name = name ; } public String toString(){ return name + " 同学 年龄为 "+ age ; } public int compareTo(Object obj){ return this.name.compareTo(((Student)obj).name); } public boolean equals(Object o){ if(!(o instanceof Student)){ return false ; } return ((Student)o).name==this.name && ((Student)o).age==this.age; }}class StudentAgeComparator implements Comparator{ public int compare(Object obj1 ,Object obj2){ Student stu1 = (Student)obj1 ; Student stu2 = (Student)obj2 ; return (stu1.age > stu2.age)?1:((stu1.age== stu2.age)?0:-1) ; }}
能正确运行,但是上面自己构建的tudentAgeComparator比较器为什么只要实现接口的一个方法,还有个equals方法没有实现,为什么会不出错
class Student implements Comparable { int age ; String name ; Student(String name , int age ){ this.age = age ; this.name = name ; } public String toString(){ return name + " 同学 年龄为 "+ age ; } public int compareTo(Object obj){ return this.name.compareTo(((Student)obj).name); } public boolean equals(Object o){ if(!(o instanceof Student)){ return false ; } return ((Student)o).name==this.name && ((Student)o).age==this.age; }}class StudentAgeComparator implements Comparator{ public int compare(Object obj1 ,Object obj2){ Student stu1 = (Student)obj1 ; Student stu2 = (Student)obj2 ; return (stu1.age > stu2.age)?1:((stu1.age== stu2.age)?0:-1) ; }}
能正确运行,但是上面自己构建的tudentAgeComparator比较器为什么只要实现接口的一个方法,还有个equals方法没有实现,为什么会不出错
只看这个自定义的比较器,前面的东西都不要看了 ,前面就是有个student类,包括姓名和年龄两个属性,把student的多个对象放进ArrayList,现在要对这个ArrayList里面的内容排序
Collections.sort(stus,new StudentAgeComparator());//就是这句话,排序没有使用默认的排序方式,而是自定义了一个比较器,这个比较器实现了Comparator接口中的一个方法,还有一个equals只字未提,但是Collections.sort(stus,new StudentAgeComparator())这个实例化了它却没有出错