當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Comparator thenComparingDouble()用法及代碼示例


Java中Comparator接口的thenComparingDouble(java.util.function.ToDoubleFunction)方法將返回字典順序比較器,並帶有提取雙精度排序鍵的函數。如果要對比較方法中相等的那些值應用另一個比較,則在比較方法之後應用此方法。

用法:

default Comparator <T> thenComparingDouble(
    ToDoubleFunction <T> keyExtractor)

參數:此方法接受keyExtractor,該函數用於提取Double排序鍵。


返回值:此方法返回一個字典順序比較器,由該字典比較器和Double排序鍵組成。

異常:如果參數為null,則此方法將引發NullPointerException。

以下示例程序旨在說明thenComparingDouble(java.util.function.ToDoubleFunction)方法:
示例1:

// Java program to demonstrate Comparator 
// thenComparingDouble(ToDoubleFunction) method 
  
import java.util.Arrays; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.List; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        List<Student> list = getStudentList(); 
  
        Comparator<Student> comparator 
            = Comparator 
                  .comparing(Student::getSchool) 
                  .thenComparingDouble(Student::getpercentageMarks); 
  
        Collections.sort(list, comparator); 
        System.out.println("After sort"); 
        list.forEach(s -> System.out.println(s)); 
    } 
  
    public static List<Student> getStudentList() 
    { 
  
        Student s1 = new Student("Ram", 85.5, "SJV"); 
        Student s2 = new Student("Shyam", 83.25, "MSH"); 
        Student s3 = new Student("Mohan", 86.55, "SJV"); 
        Student s4 = new Student("Sohan", 81.00, "MSH"); 
        Student s5 = new Student("Rabi", 55.6, "SJV"); 
        List<Student> list = Arrays.asList(s1, s2, s3, s4, s5); 
  
        return list; 
    } 
} 
  
class Student { 
  
    private String name; 
    private double percentageMarks; 
    private String school; 
  
    public Student(String name, 
                   double percentageMarks, 
                   String school) 
    { 
        this.name = name; 
        this.percentageMarks = percentageMarks; 
        this.school = school; 
    } 
  
    public String getName() 
    { 
        return name; 
    } 
  
    public void setName(String name) 
    { 
        this.name = name; 
    } 
  
    public double getpercentageMarks() 
    { 
        return percentageMarks; 
    } 
  
    public void setpercentageMarks(int percentageMarks) 
    { 
        this.percentageMarks = percentageMarks; 
    } 
  
    public String getSchool() 
    { 
        return school; 
    } 
  
    public void setSchool(String school) 
    { 
        this.school = school; 
    } 
  
    @Override
    public String toString() 
    { 
        return "Student [name=" + name + ",  
            percentageMarks 
            = " + percentageMarks  
              + ", school=" + school + "]"; 
    } 
}

IDE的控製台上打印的輸出如下所示。
輸出:

您可以在示例中看到,首先對學校進行排序,如果學校相同,則對百分比進行明智排序。

示例2:

// Java program to demonstrate Comparator 
// thenComparingDouble(ToDoubleFunction)  method 
  
import java.util.Arrays; 
import java.util.Comparator; 
import java.util.List; 
  
public class GFG { 
  
    public static void main(String... args) 
    { 
  
        List<Integer> list 
            = Arrays.asList(1, 2, 3, 4, 5, 6); 
  
        try { 
  
            // apply thenComparingDouble 
            Comparator.comparing(list::get) 
                .thenComparingDouble(null); 
        } 
        catch (Exception e) { 
  
            System.out.printf("Exception:" + e); 
        } 
    } 
}

控製台上打印的輸出如下所示。
輸出:

參考文獻: https://docs.oracle.com/javase/10/docs/api/java/util/Comparator.html#thenComparingDouble(java.util.function.ToDoubleFunction)()



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Comparator thenComparingDouble() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。