引用类型的内置类大小比较(用Comparator接口实现)——高淇JAVA300讲笔记之Comparator接口
提供排序的比较器,业务比较器。
——实现java.util.Comparator接口
——重写public int compare(T o1, T o2);
作用:
——解耦:独立于实体类
——方便:便于应对各种排序规则
我个人的理解就是,通过重写compare方法自己来定义比较大小的方法,自己想怎么比就怎么比,自由度很大,比如根据字符串的长度来比较大小。返回值同样也是正数、负数、0。
直接看例子吧。
先写一个实现了java.util.Comparator接口的类StringComp,作用是按字符串的长度比较大小。
1 package com.bjsxt.sort.innerType; 2 3 /** 4 * 排序规则的业务类 5 * 6 */ 7 public class StringComp implements java.util.Comparator<String> { 8 /** 9 * 按长度比较大小 10 * 正数 > 11 * 负数 < 12 * 0 == 13 */ 14 public int compare(String o1, String o2) { 15 int len1 = o1.length(); 16 int len2 = o2.length(); 17 18 return len1 - len2; 19 } 20 }
然后写一个工具类Utils,里面提供了数组的排序和List的排序方法。
1 package com.bjsxt.sort.innerType; 2 3 import java.util.Comparator; 4 import java.util.List; 5 6 /** 7 * 排序 8 * 9 */ 10 public class Utils { 11 /** 12 * List的排序+比较器 13 * @param list 14 * @param com 15 */ 16 public static <T> void sort(List<T> list, Comparator<T> com) { 17 //第一步:转成数组 18 Object[] arr = list.toArray(); 19 sort(arr,com); 20 //第二步:改变容器中对应的值 21 for(int i=0;i<arr.length;i++) { 22 list.set(i, (T)(arr[i])); 23 } 24 } 25 26 /** 27 * 数组的排序(降序)+ Comparator接口 28 * @param arr 29 */ 30 public static <T> void sort(Object[] arr, Comparator<T> com) { 31 //从大到小排序 32 boolean sorted = true; 33 int len = arr.length; 34 for(int j=0;j<len-1;j++) { 35 sorted = true; 36 for(int i=0;i<len-1-j;i++) { 37 if(com.compare((T)arr[i], (T)arr[i+1])<0) { 38 Object temp = arr[i]; 39 arr[i] = arr[i+1]; 40 arr[i+1] = temp; 41 sorted = false; 42 } 43 } 44 if(sorted) { 45 break; 46 } 47 } 48 } 49 50 }
最后,用一下工具类里面的方法。
1 package com.bjsxt.sort.innerType; 2 3 import java.util.ArrayList; 4 import java.util.Arrays; 5 import java.util.Date; 6 import java.util.List; 7 8 public class Demo04 { 9 public static void main(String[] args) { 10 System.out.println("===========使用Comparator排序数组================"); 11 String[] arr2 = new String[]{"a","abcd","abc","def"}; 12 Utils.sort(arr2,new StringComp()); 13 System.out.println(Arrays.toString(arr2)); 14 15 System.out.println("=========List排序+比较器=========="); 16 List<String> list = new ArrayList<String>(); 17 list.add("a"); 18 list.add("abcd"); 19 list.add("abc"); 20 list.add("def"); 21 Utils.sort(list,new StringComp()); 22 System.out.println(list); 23 24 } 25 }
运行结果:
===========使用Comparator排序数组================ [abcd, abc, def, a] =========List排序+比较器========== [abcd, abc, def, a]
JDK中的Collections类
JDK中本身就已经提供了一个Collections类可以给我们直接用。有两种用法,一种是传比较器,另一种是不传比较器。
让我们来看一下怎么使用。
1 package com.bjsxt.sort.innerType; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.List; 6 7 /** 8 * 使用Collections对容器的比较 9 * 10 */ 11 public class Demo05 { 12 public static void main(String[] args) { 13 List<String> list = new ArrayList<String>(); 14 list.add("a"); 15 list.add("abcd"); 16 list.add("abc"); 17 list.add("def"); 18 Collections.sort(list,new StringComp()); 19 System.out.println(list); 20 21 list = new ArrayList<String>(); 22 list.add("a"); 23 list.add("abcd"); 24 list.add("abc"); 25 list.add("def"); 26 Collections.sort(list); 27 System.out.println(list); 28 } 29 }
运行结果:
[a, abc, def, abcd]
[a, abc, abcd, def]