📜  Java中的比较器 thenComparingDouble() 方法及示例

📅  最后修改于: 2022-05-13 01:55:38.633000             🧑  作者: Mango

Java中的比较器 thenComparingDouble() 方法及示例

Java 中 Comparator 接口的 thenComparingDouble( Java Java 函数 )方法返回一个字典顺序比较器,该比较器带有一个提取双排序键的函数。如果您想对比较方法中相等的值应用另一个比较,则在比较方法之后应用此方法。

句法:

default Comparator  thenComparingDouble(
    ToDoubleFunction  keyExtractor)

参数:此方法接受keyExtractor ,这是用于提取双排序键的函数。

返回值:此方法返回由 this 和 Double 排序键组成的字典顺序比较器。

异常:如果参数为空,此方法将抛出 NullPointerException。

下面的程序说明 thenComparingDouble(Java 函数.ToDoubleFunction) 方法:
方案一:

// 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 list = getStudentList();
  
        Comparator 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 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 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 控制台上的输出如下所示。
输出:

您可以在示例中看到第一次排序是根据学校进行的,如果学校相同,那么百分比标记是明智的。

方案二:

// 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 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: Java Java 函数)()