<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ### 1\. Comparable ~~~ package java.lang; import java.util.*; public interface Comparable<T> { ? ?public int compareTo(T o); } ~~~ 說明: Comparable 是排序接口。若一個類實現了Comparable接口,則該類可以支持排序。 假設現在存在實現Comparable接口的類的實例的List列表(或數組),則該List列表(或數組)可以通過?Collections.sort(或 Arrays.sort)進行排序。 假設我們通過 x.compareTo(y) 來“比較x和y的大小”。若返回“負數”,意味著“x比y小”;返回“零”,意味著“x等于y”;返回“正數”,意味著“x大于y”。 舉例: ~~~ package com.qunar.test; /** * Created by xiaosi on 16-3-7. */ public class Student implements Comparable<Student>{ ? ?private String name; ? ?private int age; ? ?public Student(String name, int age) { ? ? ? ?this.name = name; ? ? ? ?this.age = age; ? ?} ? ?public String getName() { ? ? ? ?return name; ? ?} ? ?public void setName(String name) { ? ? ? ?this.name = name; ? ?} ? ?public int getAge() { ? ? ? ?return age; ? ?} ? ?public void setAge(int age) { ? ? ? ?this.age = age; ? ?} ? ?@Override ? ?public int compareTo(Student stu) { ? ? ? ?if(age == stu.getAge()){ ? ? ? ? ? ?return name.compareTo(stu.getName()); ? ? ? ?}//if ? ? ? ?else if(age > stu.getAge()){ ? ? ? ? ? ?return 1; ? ? ? ?} ? ? ? ?return -1; ? ?} } ~~~ ~~~ ? ? ? ?List<Student> stus = new ArrayList<Student>(); ? ? ? ?stus.add(new Student("xiaosi",24)); ? ? ? ?stus.add(new Student("sunny",24)); ? ? ? ?stus.add(new Student("yoona",21)); ? ? ? ?stus.add(new Student("kim",27)); ? ? ? ?Collections.sort(stus); ? ? ? ?for(Student stu : stus){ ? ? ? ? ? ?System.out.println("age" + stu.getAge() + " ? name->" + stu.getName()); ? ? ? ?} ~~~ 以上實例實現的功能是:按student的age排序,如果年齡相同,則按name排序。 ### 2\. Comparator ~~~ package java.util; public interface Comparator<T> { ? ?int compare(T o1, T o2); ? ?boolean equals(Object obj); } ~~~ 說明: 若一個類本身不支持排序,并且沒有實現Comparable接口。那么我們可以建立一個該類的比較器來進行排序。這個比較器只需要實現Comparator接口即可。我們可以通過比較器,然后通過該比較器對類進行排序。 舉例: ~~~ package com.qunar.test; /** * Created by xiaosi on 16-3-7. */ public class Teacher { ? ?private String name; ? ?private int age; ? ?public Teacher(String name, int age) { ? ? ? ?this.name = name; ? ? ? ?this.age = age; ? ?} ? ?public String getName() { ? ? ? ?return name; ? ?} ? ?public void setName(String name) { ? ? ? ?this.name = name; ? ?} ? ?public int getAge() { ? ? ? ?return age; ? ?} ? ?public void setAge(int age) { ? ? ? ?this.age = age; ? ?} } ~~~ 比較器: ~~~ package com.qunar.test; import java.util.Comparator; /** * Created by xiaosi on 16-3-7. * Teacher比較器 */ public class TeacherComparator implements Comparator<Teacher>{ ? ?@Override ? ?public int compare(Teacher o1, Teacher o2) { ? ? ? ?if(o1.getAge() == o2.getAge()){ ? ? ? ? ? ?return o1.getName().compareTo(o2.getName()); ? ? ? ?}//if ? ? ? ?else if(o1.getAge() > o2.getAge()){ ? ? ? ? ? ?return 1; ? ? ? ?} ? ? ? ?return -1; ? ?} } ~~~ ~~~ ? ? ? ?List<Teacher> teachers = new ArrayList<Teacher>(); ? ? ? ?teachers.add(new Teacher("xiaosi",24)); ? ? ? ?teachers.add(new Teacher("sunny",24)); ? ? ? ?teachers.add(new Teacher("yoona",21)); ? ? ? ?teachers.add(new Teacher("kim",27)); ? ? ? ?Collections.sort(teachers,new TeacherComparator()); ? ? ? ?for(Teacher te : teachers){ ? ? ? ? ? ?System.out.println("age" + te.getAge() + " ? name->" + te.getName()); ? ? ? ?} ~~~ ### 3\. 比較 Comparable?是一個對象本身就已經支持自比較所需要實現的接口(如 String、Integer 自己就可以完成比較大小操作)。?Comparator?是一個專用的比較器,當這個對象不支持自比較或者自比較函數不能滿足你的要求時,你可以寫一個比較器來完成兩個對象之間大小的比較。可以說一個是自己完成比較,一個是外部程序實現比較的差別而已。 用 Comparator 是策略模式(strategy design pattern),就是不改變對象自身,而用一個策略對象(strategy object)來改變它的行為。比如:你想對整數采用絕對值大小來排序,Integer 是不符合要求的,你不需要去修改 Integer 類(實際上你也不能這么做)去改變它的排序行為,只要使用一個實現了 Comparator 接口的對象來實現控制它的排序就行了。 Comparable使用:將比較方法寫到實體類內對外聲明所有比較規則統一按照這一種方式,代碼的通用性差,如果改變一種比較規則,代碼需要重寫 Comparator使用:這種方式的比較實現了實體和比較規則的解綁,實現的比較規則可以根據自己的業務需求調用對應的比較類
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看