Java中Comparator接口的compareDouble(java.util.function.ToDoubleFunction)方法接受一个函数作为参数,该参数从类型T中提取一个双精度排序键,并返回一个与该排序键进行比较的Comparator。指定的函数也可以序列化。
用法:
static <T> Comparator<T> comparingDouble( ToDoubleFunction <T> keyExtractor)
参数:此方法接受单个参数keyExtractor,该函数用于提取双精度排序键。
返回值:此方法返回一个比较器,该比较器通过提取的键进行比较
异常:如果参数为null,则此方法将引发NullPointerException。
以下示例程序旨在说明compareingDouble(java.util.function.ToDoubleFunction)方法:
示例1:
// Java program to demonstrate Comparator
// comparingDouble(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)
{
// create some user objects
User u1 = new User("Aaman", 3500.56);
User u2 = new User("Joyita", 4222.99);
User u3 = new User("Suvam", 2832.34);
User u4 = new User("mahafuj", 2522.76);
// before sort
List<User> list = Arrays.asList(u2, u1, u4, u3);
System.out.println("Before Sort:");
list.forEach(user -> System.out.println(user));
Collections.sort(list,
Comparator.comparingDouble(User::getSalary));
System.out.println("\nAfterSort:");
list.forEach(user -> System.out.println(user));
}
}
class User implements Comparable<User> {
public String name;
public double salary;
public User(String name, double d)
{
this.name = name;
this.salary = d;
}
public int compareTo(User u1)
{
return name.compareTo(u1.name);
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public double getSalary()
{
return salary;
}
public void setSalary(double salary)
{
this.salary = salary;
}
@Override
public String toString()
{
return "User [name=" + name
+ ", salary="
+ salary + "]";
}
}
IDE的控制台上打印的输出如下所示。
输出:
示例2:
// Java program to demonstrate Comparator
// comparingDouble(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)
{
// before sort
List<Order> list = Arrays.asList(
new Order("A382y482y48", 32684.82),
new Order("Vvekhfbkje2", 28348.47),
new Order("efkhfbekjfbe", 47674.49),
new Order("bhdhdfaddvad", 78449.43),
new Order("abkasbcjabjc", 10104.43));
System.out.println("Before Sort:");
list.forEach(order -> System.out.println(order));
Collections.sort(list,
Comparator.comparingDouble(Order::getValue));
System.out.println("\nAfter Sort:");
list.forEach(order -> System.out.println(order));
}
}
class Order implements Comparable<Order> {
public String orderNo;
public double value;
public int compareTo(Order o1)
{
return orderNo.compareTo(o1.orderNo);
}
public Order(String orderNo, double value)
{
super();
this.orderNo = orderNo;
this.value = value;
}
@Override
public String toString()
{
return "Order [orderNo=" + orderNo
+ ", value=" + value + "]";
}
public String getOrderNo()
{
return orderNo;
}
public void setOrderNo(String orderNo)
{
this.orderNo = orderNo;
}
public double getValue()
{
return value;
}
public void setValue(double value)
{
this.value = value;
}
}
控制台上打印的输出如下所示。
输出:
相关用法
- Java Collectors minBy(Comparator comparator)用法及代码示例
- Java Collectors maxBy(Comparator comparator)用法及代码示例
- Java Stream sorted(Comparator comparator)用法及代码示例
- Java Comparator reverseOrder()用法及代码示例
- Java ConcurrentSkipListSet comparator()用法及代码示例
- Java Comparator thenComparingDouble()用法及代码示例
- Java Comparator nullsLast()用法及代码示例
- Java SortedMap comparator()用法及代码示例
- Java Comparator thenComparingInt()用法及代码示例
- Java TreeMap comparator()用法及代码示例
- Java Comparator comparingLong()用法及代码示例
- Java Comparator nullsFirst()用法及代码示例
- Java Comparator naturalOrder()用法及代码示例
- Java Comparator reversed()用法及代码示例
- Java Comparator thenComparingLong()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Comparator comparingDouble() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。