Java中Comparable与Comparator的区别
1、Comparator
Comparator是比较接口,我们如果需要控制某个类的次序,而该类本身不支持排序(即没有实现Comparable接口),那么我们就可以建立一个“该类的比较器”来进行排序,(n1,n2) => n1<n2 return 1 降序;n1==n2 return 1 原序
数组如何使用Comparator,比喻nums[3][4],Arrays.sort(nums, Comparator.comparingInt(t -> t[2]));
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class StudentComparator { public static void main(String args[]) { List<StudentComparator> list = new ArrayList<StudentComparator>(); for (int i = 10; i > 0; i--) { list.add(new StudentComparator(i)); } Collections.sort(list,new MenuComparator()); for (StudentComparator m : list) { System.out.println(m.getId()); } } public static class MenuComparator implements Comparator<StudentComparator> { public int compare(StudentComparator o1, StudentComparator o2) { if (null != o1 && null != o2) { StudentComparator menu1 = (StudentComparator) o1; StudentComparator menu2 = (StudentComparator) o2; if (menu1.getId() > menu2.getId()) { return 1; } else { return 0; } } return 0; } } private int id; private int age; private String name; public StudentComparator(int id) { this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
import java.util.Comparator; import java.util.PriorityQueue; import java.util.Queue; class Element{ public int row,col,val; public Element(int row, int col, int val) { this.row = row; this.col = col; this.val = val; } } public class Main { private Comparator<Element> MyCmp = new Comparator<Element>() { @Override //升序 public int compare(Element o1, Element o2) { return o1.val - o2.val; } }; public int[] mergeKSortedArrays(int[][] arr) { if(arr == null) { return new int[0]; } int sum = 0; Queue<Element> q = new PriorityQueue<Element>( arr.length,MyCmp); for(int i = 0; i < arr.length; i++) { if(arr[i].length > 0) { Element e = new Element(i, 0, arr[i][0]); q.add(e); sum += arr[i].length; //记录结果数组总长度 } } int[] res = new int[sum]; int idx = 0; while(!q.isEmpty()) { Element e = q.poll(); //弹出堆顶最小值 res[idx++] = e.val; // 当前结点被 PriorityQueue 抛出来后,当前行的第二个结点加入 PriorityQueue if(e.col + 1 < arr[e.row].length) { //将当前最小所在数组的下一个元素存入pq q.add(new Element(e.row, e.col+1, arr[e.row][e.col+1])); } } return res; } public static void main(String[] args) { Main m = new Main(); int[][] arr = {{1, 3, 5, 7},{2, 4, 6},{0, 8, 9, 10, 11}}; int[] res = m.mergeKSortedArrays(arr); for(int i=0; i<res.length; i++) { System.out.print(res[i] + " "); } } } /* Input: [ [1, 3, 5, 7], [2, 4, 6], [0, 8, 9, 10, 11] ] Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] */
class Solution { public ListNode mergeKLists(ListNode[] lists) { if(lists==null){ return null; } ListNode megerHead=new ListNode(-1); ListNode cur=megerHead; PriorityQueue<ListNode> queue=new PriorityQueue<>(new Comparator<ListNode>(){ public int compare(ListNode o1,ListNode o2){ return o1.val-o2.val; } }); for(ListNode l:lists){ if(l!=null){ queue.offer(l); } } while(!queue.isEmpty()){ ListNode p=queue.poll(); cur.next=p; cur=cur.next; if(p.next!=null){ queue.offer(p.next); } } return megerHead.next; } }
2、Comparable
Comparable是排序接口。若一个类实现了Comparable接口,就意味着该类支持排序。实现了Comparable接口的类的对象的列表或数组可以通过Collections.sort或Arrays.sort进行自动排序。
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class StudentComparable implements Comparable { public static void main(String args[]) { List<StudentComparable> list = new ArrayList<StudentComparable>(); for (int i = 10; i > 0; i--) { list.add(new StudentComparable(i)); } Collections.sort(list); for (StudentComparable s : list) { System.out.println(s.getId()); } } public int compareTo(Object o) { if (o instanceof StudentComparable) { StudentComparable s = (StudentComparable) o; if (this.id > s.id) { return 1; } else { return 0; } } return -1; } private int id; private int age; private String name; public StudentComparable(int id) { this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }