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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。