让需要进行排序的对象实现Comparable接口,重写其中的compareTo(Object obj)方法,在其中定义排序规则,那么就可以直接调用java.util.Arrays.sort()来排序对象数组,当用集合例如ArrayList代替数组时,可直接调用Collections.sort();
public int compareTo(Object obj) { AutoStudent ast = (AutoStudent) obj; if(this.height==ast.height) { return ast.name.compareTo(this.name); } else { return this.height-ast.height;//身高按从低到高 } }Comparator接口中必须要实现的compare(Object obj1,Object obj2 )有两个参数。
public class StudentComparactor implements Comparator<StudentCom> { public int compare(StudentCom sc1, StudentCom sc2) { if(sc1.getHeight().compareTo(sc2.getHeight())>0) return 1; if(sc1.getHeight().compareTo(sc2.getHeight())<0) return -1; else { if(sc1.getName().compareTo(sc2.getName())>0) return 1; if(sc1.getName().compareTo(sc2.getName())<0) return -1; } return 0; } }
package ch7; public class AutoStudent implements Comparable<AutoStudent>{ //<Object> private String name; private int height; AutoStudent () {} AutoStudent(String name,int height) { this.name=name; this.height=height; } public void setName(String name) { this.name=name; } public String getName() { return name; } public void setHeight(int height) { this.height=height; } public int getHeight() { return height; } public String toString() { return name+"同学的身高是:"+height+"厘米"; } public boolean equals(Object obj) //重写equals方法 { if(obj instanceof AutoStudent) { AutoStudent r = (AutoStudent) obj; if(r.name.equals(this.name)&&r.height==this.height) return true; } return false; } public int hashCode(){ //重写hashCode方法 return name.hashCode()^height; } public int compareTo(AutoStudent ast) { if(this.height==ast.height) { return this.name.compareTo(ast.name); } else { return this.height-ast.height; //按从低到高排列 } } /* public int compareTo(Object obj) { AutoStudent ast = (AutoStudent) obj; if(this.height==ast.height) { return ast.name.compareTo(this.name); } else { return this.height-ast.height;//身高按从低到高 } } */ }
package ch7; import java.util.*; public class AutoStudentText { public static void main(String[] args) { List <AutoStudent> list = new ArrayList <AutoStudent> (); AutoStudent s1 = new AutoStudent("liming",167); AutoStudent s2 = new AutoStudent("libai",155); AutoStudent s3 = new AutoStudent("caicai",180); AutoStudent s4 = new AutoStudent("weilai",160); AutoStudent s5 = new AutoStudent("chali",160); list.add(s1); list.add(s2); list.add(s3); list.add(s4); list.add(s5); Collections.sort(list); Iterator <AutoStudent>it = list.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
package ch7; public class StudentCom { private Integer height; private String name; public StudentCom() {}//无参构造方法 public StudentCom(Integer height, String name) { //有参构造方法 this.height = height; this.name = name; } public Integer getHeight() { return height; } public void setHeight(Integer height) { this.height = height; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { //重写toString方法 return name + "同学的身高是: " + height +"厘米"; } @Override public boolean equals(Object obj) { //重写equals方法 if(obj instanceof StudentCom) { StudentCom ast=(StudentCom)obj; if(ast.name.equals(this.name) && ast.height==this.height) { return true; } } return false; } @Override public int hashCode() { //重写hashCode方法 return name.hashCode()^height; } }
package ch7; import java.util.Comparator; /* * 构建一个StudentComparator类,实现Comparator接口,用来在构建集合的时候传入 * 按照身高进行排列,身高相同的按照名字的字典顺序排列 */ public class StudentComparactor implements Comparator<StudentCom> { public int compare(StudentCom sc1, StudentCom sc2) { if(sc1.getHeight().compareTo(sc2.getHeight())>0) return 1; if(sc1.getHeight().compareTo(sc2.getHeight())<0) return -1; else { if(sc1.getName().compareTo(sc2.getName())>0) return 1; if(sc1.getName().compareTo(sc2.getName())<0) return -1; } return 0; } }
package ch7; import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet; public class TestComparator { public static void main(String[] args) { //构建集合,传入学生比较器 SortedSet<StudentCom> ss = new TreeSet<StudentCom>(new StudentComparactor()); StudentCom sc1 = new StudentCom(160,"lily"); StudentCom sc2 = new StudentCom(165,"zhangsan"); StudentCom sc3 = new StudentCom(172,"wangwu"); StudentCom sc4 = new StudentCom(193,"lisi"); StudentCom sc5 = new StudentCom(178,"zhaoliu"); StudentCom sc6 = new StudentCom(180,"zhuba"); StudentCom sc7 = new StudentCom(172,"mike"); StudentCom sc8 = new StudentCom(172,"jim"); StudentCom sc9 = new StudentCom(172,"andy"); //向集合ss中添加元素,即学生站到队伍里 ss.add(sc1); ss.add(sc2); ss.add(sc3); ss.add(sc4); ss.add(sc5); ss.add(sc6); ss.add(sc7); ss.add(sc8); ss.add(sc9); //遍历输出,按照要求排序 Iterator <StudentCom>it=ss.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }