1 / 9

Comparator<T> 

Comparator<T> . Comparator<T>. Comparator <T> 它是 java.util 底下的 interface 他是用在 object 與 object 之間的值之比較 雖然他只是個 interface 但當我們要使用他時 必須先定義比較的規則 必須實作他唯一的 method: public int compare(T t1,T,t2) 且在裡面定義比較的規則 如果能善用他 我們可以省去許多不必要的 code. Example 1. import java.util .*; class comparator{

Download Presentation

Comparator<T> 

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Comparator<T> 

  2. Comparator<T> • Comparator <T>它是java.util 底下的interface • 他是用在object與object之間的值之比較 • 雖然他只是個interface 但當我們要使用他時 必須先定義比較的規則 • 必須實作他唯一的method: public int compare(T t1,T,t2) 且在裡面定義比較的規則 • 如果能善用他 我們可以省去許多不必要的code

  3. Example 1 import java.util.*; class comparator{ public static void main(String[] args){ TreeSettreeset=new TreeSet(new newComparator()); treeset.add("Item a"); treeset.add("Item b"); treeset.add("Item c"); treeset.add("Item d"); treeset.add("Item e"); treeset.add("Item f"); treeset.add("Item g"); System.out.println(treeset); } }

  4. class newComparator implements Comparator{ public int compare(Object obj1,Object obj2){ if(((String)obj1).equals("Item c")) return -1; return ((String)obj1).compareTo((String)obj2); } } Ans:[Item c, Item a, Item b, Item d, Item e, Item f, Item g] 1.int compare()只能return 1,0,-1三個值 2.-1:排前面 1:排後面 0:代表不care 3.compareTo()代表字串裡面的字元ACSII比較 假如第一個字元相同就比第二個 第二個字元相同就比第三個 以下類推 小的=-1排前面 大的=1排後面 相等=0:不care 4.如果沒有指定泛型(generic type)則comapre參數必以Object宣告

  5. 概念解說: initial compare Step1. a,b,c,d,e,f,g (還沒比為空) Step2. a,b,d,e,f,g c (if(((String)obj1).equals("Item c")) return -1) Step3. b,d,e,f,g c,a(a的ACSII碼最小) Step4. d,e,f,g c,a,b(a<b)比acsii Step5. e,f,g c,a,b,d(a<b<d) Step6. f,g c,a,b,d,e(a<b<d<e) Step7. g c,a,b,d,e,f(a<b<d<e<f) Step8. (空) c,a,b,d,e,f,g(c<a<b<d<e<f<g)

  6. Example2 import java.util.*; public class a{ public static void main(String[] args){ String[] num={"5","0","7","4","9","2","1","3","6","8"}; Node[] node=new Node[num.length]; for(int i=0;i<num.length;i++){ node[i]=new Node(num[i]); } Arrays.sort(node,new newComparator()); for(int i=0;i<num.length;i++) System.out.print(node[i].getNum()); System.out.println(); } }

  7. class newComparator implements Comparator<Node>{ public int compare(Node num1,Node num2){ if(num1.getNum()>num2.getNum()) return 1; else if(num1.getNum()<num2.getNum()) return -1; else return 0; } } //這是用來定義怎樣比較的class num1:要拿來比較的element num2:已排序好的element 從index=0開始跟num1比較 直到index=length-1 1:排後面 -1:排前面 0:不care (此程式是由小排到大) 因為泛型(generic type是Node 所以compare裡的參數必以Node 宣告)

  8. class Node{ int num; public Node(String num){ this.num=Integer.parseInt(num); } public int getNum(){ return this.num; } } Output:0 1 2 3 4 5 6 7 8 9

  9. 概念解說(左:尚未排序 右:已排序) 5,0,7,4,9,2,1,3,6,8 (空) 0,7,4,9,2,1,3,6,8(從第一個比)5 7,4,9,2,1,3,6,8 0,5(0<5 return -1) 4,9,2,1,3,6,8 0,5,7(0<7 return 1,5<7 return 1) 9,2,1,3,6,8 0,4,5,7(0<4 return 1,4<5<7 return -1) 2,1,3,6,8 0,4,5,7,9(0<4<5<7<9 return 1) 1,3,6,8 0,2,4,5,7,9(0<2 return 1,2<4<5<7<9 return -1) 3,6,8 0,1,2,4,5, 7,9(0<1 return 1,1<2<4<5<7<9 return -1) 6,8 0,1,2,3,4,5,7,9(0<1<2<3 return 1,3<4<5<7<9 return -1) 8 0,1,2,3,4,5,6,7,9(0<1<2<3<4<5<6 return 1,6<7<9 return -1) (空) 0,1,2,3,4,5,6,7,8,9(0<1<2<3<4<5<6<7<8 return 1,8<9 return -1)

More Related