标签:
---Comparable接口----- public interface Comparable<T> { int compareTo(T t); } ---Comparable接口-----
Collections.sort
(and Arrays.sort
). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.public class Employee implements Comparable<Employee> { private int EmpID ; private String Ename; private double Sal ; private static int i; public Employee() { EmpID = i++; Ename = "dont know"; Sal = 0.0; } public Employee(String ename, double sal) { EmpID = i++; Ename = ename; Sal = sal; } public String toString() { return "EmpID " + EmpID + "\n" + "Ename " + Ename + "\n" + "Sal " + Sal ; } public int compareTo(Employee arg0) { // TODO Auto-generated method stub if (Sal < arg0. Sal) return -1; else if (Sal == arg0.Sal) return 0; else return 1; } }
然后就可以使用该类了,结合List<T>,Array集合实现对集合中的元素进行排序,如下:
public class ComparableDemo { public static void main(String[] args) { List<Employee> ts1 = new ArrayList<Employee>(); ts1.add(new Employee( "Tom", 40000.00)); ts1.add(new Employee( "Harry", 20000.00)); ts1.add(new Employee( "Maggie", 50000.00)); ts1.add(new Employee( "Chris", 70000.00)); Collections.sort(ts1); Iterator<Employee> itr = ts1.iterator(); System.out.println( "------List--------"); while (itr.hasNext()) { Object element = itr.next(); System. out.println(element + "\n" ); } System.out.println( "-----Array--------"); Employee[] oneArray = { new Employee("Peter" , 30000.00), new Employee( "Harry", 20000.00), new Employee("Maggie" , 50000.00)}; Arrays.sort(oneArray); for (Employee one : oneArray) { System. out.println(one + "\n" ); } } }
Employee one = new Employee( "Tom", 40000.00); Employee two = new Employee("Chris", 70000.00); if (one.compareTo( two) == -two .compareTo(one)) { System. out.println("satisfy rule one." ); }
Employee x = new Employee( "Tom", 40000.00); Employee y = new Employee( "ChrisY", 30000.00); Employee z = new Employee( "Chris", 20000.00); if (x.compareTo(y) > 0 && y.compareTo(z) > 0 && x.compareTo(z) > 0) { System. out.println("satisfy rule No.two" ); }
Employee x = new Employee( "Tom", 40000.00); Employee y = new Employee( "ChrisY", 40000.00); Employee z = new Employee( "Chris", 20000.00); if (x.compareTo(y) == 0) { if (x.compareTo(z) == y.compareTo(z)) { System. out.println("satisfy rule No.threee" ); } }
public class NewEmployee implements Comparable<NewEmployee> { public static final int Low = 10; public static final int Middle = 20; public static final int High = 30; private int EmpID ; private String Ename; private double Sal ; private static int i; private Grade oneGrade; public NewEmployee() { EmpID = i++; Ename = "dont know"; Sal = 0.0; oneGrade = new Grade( Low ); } public NewEmployee(String ename, double sal, Grade grade) { EmpID = i++; Ename = ename; Sal = sal; oneGrade = grade; } public String toString() { return "EmpID " + EmpID + "\n" + "Ename " + Ename + "\n" + "Sal " + Sal + "\n" + "Grade " + getGradeName( oneGrade.getGrade()); } private String getGradeName( int grade) { switch (grade) { case Low : return "Low" ; case Middle : return "Middle" ; case High : return "High" ; default: return "Null" ; } } public int compareTo(NewEmployee arg0) { // TODO Auto-generated method stub if ( oneGrade.compareTo(arg0. oneGrade) == 0) { if ( Sal < arg0. Sal) { return -1; } else if (Sal == arg0. Sal) { return 0; } else { return 1; } } else { return oneGrade.compareTo(arg0. oneGrade); } } } -------- public class NewComparableDemo { public static void main(String[] args) { List<NewEmployee> ts1 = new ArrayList<NewEmployee>(); ts1.add( new NewEmployee( "Tom" , 40000.00, new Grade(NewEmployee. Middle))); ts1.add( new NewEmployee( "Harry" , 20000.00, new Grade(NewEmployee. Low))); ts1.add( new NewEmployee( "Maggie" , 50000.00, new Grade(NewEmployee. High))); ts1.add( new NewEmployee( "Chris" , 70000.00, new Grade(NewEmployee. Low))); Collections. sort(ts1); Iterator<NewEmployee> itr = ts1.iterator(); while (itr.hasNext()) { Object element = itr.next(); System. out .println(element + "\n" ); } NewEmployee x = new NewEmployee( "Tom" , 40000.00, new Grade(NewEmployee.Middle )); NewEmployee y = new NewEmployee( "Harry" , 20000.00, new Grade(NewEmployee.Low )); if (x.compareTo(y) == -y.compareTo(x)) { System. out .println("satisfy rule No.one" ); } NewEmployee x1 = new NewEmployee( "Tom" , 40000.00, new Grade(NewEmployee.Middle )); NewEmployee y1 = new NewEmployee( "Harry1" , 70000.00, new Grade(NewEmployee.Low )); NewEmployee z1 = new NewEmployee( "Harry2" , 30000.00, new Grade(NewEmployee.Low )); if (x1.compareTo(y1) > 0 && y1.compareTo(z1) > 0 && x1.compareTo(z1) > 0) { System. out .println("satisfy rule No.two" ); } NewEmployee x2 = new NewEmployee( "Tom2" , 70000.00, new Grade(NewEmployee.Middle )); NewEmployee y2 = new NewEmployee( "Harry2" , 70000.00, new Grade(NewEmployee.Middle )); NewEmployee z2 = new NewEmployee( "Harry2" , 30000.00, new Grade(NewEmployee.Low )); if (x2.compareTo(y2) == 0) { if (x2.compareTo(z2) == y2.compareTo(z2)) { System. out .println("satisfy rule No.three" ); } } } }
标签:
原文地址:http://www.cnblogs.com/ttylinux/p/4375789.html