一點一點看JDK源碼(五)java.util.ArrayList 後篇之sort與Comparator
html
liuyuhang原創,未經容許禁止轉載java
本文舉例使用的是JDK8的APIide
Comparator爲額外實現的比較接口,與類自己無關post
該接口在ArrayList的sort中有應用(不少時候均可以用的,只是以此舉例)this
Comparator接口主要用途有兩種,url
一、比較有序集合內任意兩個元素A、B(徹底遍歷的compareTo方法,於compare方法內實現)spa
若A元素小於B元素,則返回1,donothing3d
若A元素等於B元素,則返回0,donothingcode
若A元素小於B元素,則返回-1,交換元素位置htm
若compare方法的返回值遵循以上原則,則進行排序
示例代碼以下:
1 package com.FM.ArrayListStudy; 2 3 import java.util.ArrayList; 4 import java.util.Comparator; 5 6 public class ComparatorInArrayListStudy01 { 7 public static void main(String[] args) { 8 ArrayList<Integer> list = new ArrayList<Integer>(); 9 list.add(1); 10 list.add(12); 11 list.add(3); 12 list.add(4); 13 list.add(3); 14 list.add(11); 15 list.add(7); 16 System.out.println(list);//排序以前的list 17 18 list.sort(new Comparator<Integer>() {//使用Comparator進行天然排序 19 @Override 20 public int compare(Integer one, Integer anotherOne) { 21 int compareTo = one.compareTo(anotherOne);//正序排序 22 //int compareTo = anotherOne.compareTo(one);//逆序排序 23 24 //比較結果爲1則不操做 25 //比較結果爲0則相等 26 //比較結果爲-1則交換位置 27 System.out.println(anotherOne + " -- " + one + " --- " + compareTo); 28 return compareTo; 29 } 30 }); 31 System.out.println(list);//排序以後的list 32 } 33 }
運行結果:
二、依照比較的結果(-1,0,1)進行判斷分組
示例代碼以下:Apple類
1 package com.FM.ArrayListStudy; 2 3 public class Apple { 4 5 private Integer id; 6 private Integer size; 7 8 public Integer getId() { 9 return id; 10 } 11 12 public void setId(Integer id) { 13 this.id = id; 14 } 15 16 public Integer getSize() { 17 return size; 18 } 19 20 public void setSize(Integer size) { 21 this.size = size; 22 } 23 24 public Apple(Integer id, Integer size) { 25 super(); 26 this.id = id; 27 this.size = size; 28 } 29 30 @Override 31 public String toString() { 32 return "Apple [id=" + id + ", size=" + size + "]"; 33 } 34 35 }
示例代碼以下:利用Comparator接口分組
1 package com.FM.ArrayListStudy; 2 3 import java.util.ArrayList; 4 import java.util.Comparator; 5 import java.util.List; 6 7 public class ComparatorInArrayListStudy02 { 8 public static void main(String[] args) { 9 ArrayList<Apple> list = new ArrayList<Apple>(); 10 list.add(new Apple(1, 81)); 11 list.add(new Apple(2, 76)); 12 list.add(new Apple(3, 91)); 13 list.add(new Apple(4, 84)); 14 list.add(new Apple(5, 79)); 15 list.add(new Apple(6, 87)); 16 list.add(new Apple(7, 85)); 17 list.add(new Apple(8, 83)); 18 list.add(new Apple(9, 91)); 19 System.out.println(list);//排序以前的list 20 21 List<List<Apple>> disPartList = disPart(list,new Comparator<Apple>(){ 22 @Override 23 public int compare(Apple o1, Apple o2) {//這裏寫具體的分組接口,分組方式能夠使用對象內的屬性的組合方式 24 if(o1.getSize()/10 == o2.getSize()/10){//將蘋果Apple按照size分組,每10個size分爲一組 25 return 0; 26 } 27 return 1; 28 } 29 }); 30 for(List lis : disPartList){//分別遍歷每一組 31 System.out.println(lis); 32 } 33 } 34 35 /** 36 * 按照comparator進行分組的方法 37 */ 38 public static <T> List<List<T>> disPart(List<T> list, Comparator<? super T> c) { 39 ArrayList<List<T>> resultList = new ArrayList<List<T>>(); 40 for (T t : list) { 41 boolean flag = false; 42 for (int i = 0; i < resultList.size(); i++) { 43 if (c.compare(t, resultList.get(i).get(0)) == 0) {// 若匹配成功則加入resultList中的子元素 44 flag = true; 45 resultList.get(i).add(t); 46 break; 47 } 48 } 49 if (flag == false) {// 若flag爲false則將此元素加入resultList爲新元素 50 List<T> listIn = new ArrayList<T>(); 51 listIn.add(t); 52 resultList.add(listIn); 53 } 54 } 55 return resultList; 56 } 57 }
運行結果:
分組的方式不少,不少人也喜歡本身寫遍從來分組
利用好Comparator接口進行分組能更好的重用,也更容易擴展!
以上!!