Comparator接口位于:java.util包中。
Comparator接口:
1. 强行对某个对象的Collection进行整体排序。
值得注意的是:
Comparator接口可以作为参数传到一些sort()方法中,例如:
Collection.sort(List<T> list, Comparator<? super T> c)、
Arrays.sort(T[] a, Comparator<? super T> c);
2. Comparator接口指定的顺序,可以不是字典顺序排序,但包含字典顺序排序!!!
现在让我们仍然对User进行排序吧!!!
Let's do it!!!
3. 有的人会觉得 实现Comparator接口 有点奇怪,Comparator接口中明明有两个方法: compare()、equals()
为什么实现此接口的类只用实现compare()方法呢?
其实稍加思索,就明白了:实现Comparator接口的类没有实现equals方法,是因为类里有equals()方法了,
所有的类都是从基类java.lang.Object中继承的,而Object类中已经有equals()方法了,
故而在你创建完成这个类,就已经实现了equals()方法了,是不是恍然大悟呢?!!!
User类的代码如下:
package test; public class User { private String name; private Integer age; public User(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } }
看下实现Comparator接口的类UserComparator的代码:
package test; import java.util.Comparator; public class UserComparator implements Comparator<User>{ @Override public int compare(User o1, User o2) { int flag = o1.getName().compareTo(o2.getName()); if(flag == 0) { flag = o1.getAge().compareTo(o1.getAge()); } return flag; } }
接着我们要进行测试了哈!UserTest.java的代码如下:
package test; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class UserTest { public static void print(List<User> list) { for(User u : list) { System.out.println(u); } } public static void print(User[] array) { for(int i=0; i<array.length; i++) { System.out.println(array[i]); } } public static void main(String[] args) { List<User> users = new ArrayList<User>(); users.add(new User("a",12)); users.add(new User("C",25)); users.add(new User("B",12)); users.add(new User("A",15)); users.add(new User("b",19)); users.add(new User("c",17)); users.add(new User("a",22)); User[] users2 = new User[]{ new User("a",12), new User("C",25), new User("B",12), new User("A",15), new User("b",19), new User("c",17), new User("a",22) }; System.out.println("=========================Comparator接口的测试========================="); System.out.println("List排序前:"); print(users); System.out.println(); System.out.println(); System.out.println("List排序后:"); Collections.sort(users, new UserComparator()); print(users); System.out.println("--------------------------------------------"); System.out.println(); System.out.println(); System.out.println(); System.out.println(); System.out.println("Array排序前:"); print(users2); System.out.println(); System.out.println(); System.out.println("Array排序后:"); Arrays.sort(users2, new UserComparator()); print(users2); } }
测试的结果如下:
=========================Comparator接口的测试========================= List排序前: User [name=a, age=12] User [name=C, age=25] User [name=B, age=12] User [name=A, age=15] User [name=b, age=19] User [name=c, age=17] User [name=a, age=22] List排序后: User [name=A, age=15] User [name=B, age=12] User [name=C, age=25] User [name=a, age=12] User [name=a, age=22] User [name=b, age=19] User [name=c, age=17] -------------------------------------------- Array排序前: User [name=a, age=12] User [name=C, age=25] User [name=B, age=12] User [name=A, age=15] User [name=b, age=19] User [name=c, age=17] User [name=a, age=22] Array排序后: User [name=A, age=15] User [name=B, age=12] User [name=C, age=25] User [name=a, age=12] User [name=a, age=22] User [name=b, age=19] User [name=c, age=17]
以上是按照字典顺序进行排序,今天想着实现一个字典顺序的倒序排序,大家一起来讨论下吧!!
下面是UserComparator2的代码:
package test; import java.util.Comparator; public class UserComparator2 implements Comparator<User>{ @Override public int compare(User o1, User o2) { int flag = 0; flag = sort(o1.getName(), o2.getName()); if(flag == 0) { flag = o1.getAge().compareTo(o2.getAge()); } return flag; } /* * 字典顺序的倒序排序,z是第一个,A是最后一个, * 如果A<B,返回1 * A=B,返回0 * A>B,返回-1 */ public int sort(String s1, String s2) { int flag = 0; //获取s1、s2的字符序列 char[] c1 = s1.toCharArray(); char[] c2 = s2.toCharArray(); //设置循环的次数,取字符串长度小的 int length1 = s1.length(); int length2 = s2.length(); int longer = c1.length - c2.length; int count = longer>=0 ? length2 : length1; for(int i=0; i<count; i++) { //取出每个字符串上对应的字符,进行比较 char charS1 = c1[i]; char charS2 = c2[i]; int result = charS1 - charS2; if(result < 0) { flag = 1; break; }else if(result > 0) { flag = -1; break; }else { flag = 0; } } //如果是这种情况:jim、jime,则jime排在前 if(flag == 0) { if(longer < 0) { //长度长的排前面 flag = 1; }else if(longer > 0) { flag = -1; }else { flag = 0; } } return flag; } }
然后,让我们开始进行测试吧:
package test; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class UserTest2 { public static void print(List<User> list) { for(User u : list) { System.out.println(u); } } public static void main(String[] args) { List<User> users = new ArrayList<User>(); users.add(new User("jim",12)); users.add(new User("joe",25)); users.add(new User("jime",12)); users.add(new User("tom",15)); users.add(new User("tom",17)); users.add(new User("andy",19)); users.add(new User("cwayi",17)); users.add(new User("zendy",22)); users.add(new User("yorick",33)); users.add(new User("maomi",22)); System.out.println("=========================Comparator接口的测试========================="); System.out.println("List排序前:"); print(users); System.out.println(); System.out.println(); System.out.println("List排序后:"); Collections.sort(users, new UserComparator2()); print(users); System.out.println("--------------------------------------------"); } /* * 预想的结果应该是: * zendy--22 * yorick--33 * tom--15 * tom--17 * maomi--22 * joe--25 * jime--12 * jim--12 * cwayi--17 * andy--19 */ }
程序运行的结果如下:
=========================Comparator接口的测试========================= List排序前: User [name=jim, age=12] User [name=joe, age=25] User [name=jime, age=12] User [name=tom, age=15] User [name=tom, age=17] User [name=andy, age=19] User [name=cwayi, age=17] User [name=zendy, age=22] User [name=yorick, age=33] User [name=maomi, age=22] List排序后: User [name=zendy, age=22] User [name=yorick, age=33] User [name=tom, age=15] User [name=tom, age=17] User [name=maomi, age=22] User [name=joe, age=25] User [name=jime, age=12] User [name=jim, age=12] User [name=cwayi, age=17] User [name=andy, age=19] --------------------------------------------
是不是很有意思呢?!以上就是Comparator接口的学习了,快来一起学习吧!