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);
}
}
}
控製台上打印的輸出如下所示。
輸出:
相關用法
- Java Collectors maxBy(Comparator comparator)用法及代碼示例
- Java Collectors minBy(Comparator comparator)用法及代碼示例
- Java Stream sorted(Comparator comparator)用法及代碼示例
- Java Comparator reversed()用法及代碼示例
- Java Comparator naturalOrder()用法及代碼示例
- Java Comparator thenComparingInt()用法及代碼示例
- Java Comparator thenComparingLong()用法及代碼示例
- Java Comparator comparingLong()用法及代碼示例
- Java ConcurrentSkipListSet comparator()用法及代碼示例
- Java Comparator reverseOrder()用法及代碼示例
- Java Comparator nullsFirst()用法及代碼示例
- Java Comparator comparingDouble()用法及代碼示例
- Java SortedMap comparator()用法及代碼示例
- Java TreeMap comparator()用法及代碼示例
- Java Comparator nullsLast()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Comparator thenComparingDouble() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。