Java中的Comparator compareLong()方法和例子
Java 中 Comparator 接口的 comparisonLong( Java Java.ToLongFunction )方法接受一个函数作为参数,该函数从类型 T 中提取一个长排序键,并返回一个按该排序键进行比较的比较器。返回的比较器是可序列化的,如果指定的函数也是可序列化的。
句法:
static Comparator
comparingLong(ToLongFunction keyExtractor)
参数:此方法接受单个参数keyExtractor ,它是用于提取长排序键的函数。
返回值:此方法返回一个比较器,通过提取的键进行比较
异常:如果参数为 null,则此方法抛出 NullPolongerException。
下面的程序说明了 compareLong(Java 函数.ToLongFunction) 方法:
方案一:
// Java program to demonstrate Comparator
// comparingLong(ToLongFunction) 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", 25724474);
User u2 = new User("Joyita", 2262848);
User u3 = new User("Suvam", 2824444);
User u4 = new User("mahafuj", 2544244);
// before sort
List list = Arrays.asList(u2, u1, u4, u3);
System.out.println("Before Sort:");
list.forEach(user -> System.out.println(user));
Collections.sort(list,
Comparator.comparingLong(
User::getregistrationId));
System.out.println("\nAfterSort:");
list.forEach(user -> System.out.println(user));
}
}
class User implements Comparable {
public String name;
public long registrationId;
public User(String name, long registrationId)
{
this.name = name;
this.registrationId = registrationId;
}
public int compareTo(User u1)
{
return name.compareTo(u1.name);
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public long getregistrationId()
{
return registrationId;
}
public void setregistrationId(long registrationId)
{
this.registrationId = registrationId;
}
@Override
public String toString()
{
return "User [name=" + name
+ ", registrationId="
+ registrationId + "]";
}
}
打印在 IDE 控制台上的输出如下所示。
输出:
方案二:
// Java program to demonstrate Comparator
// comparingLong(ToLongFunction) 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 list = Arrays.asList(
new Order("A382y482y48", 3204355),
new Order("Vvekhfbkje2", 2425355),
new Order("efkhfbekjfbe", 1345435),
new Order("bhdhdfaddvad", 2305535),
new Order("abkasbcjabjc", 1005454));
System.out.println("Before Sort:");
list.forEach(order -> System.out.println(order));
Collections.sort(list,
Comparator.comparingLong(
Order::getValue));
System.out.println("\nAfter Sort:");
list.forEach(order -> System.out.println(order));
}
}
class Order implements Comparable {
public String orderNo;
public long value;
public int compareTo(Order o1)
{
return orderNo.compareTo(o1.orderNo);
}
public Order(String orderNo, long 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 long getValue()
{
return value;
}
public void setValue(long value)
{
this.value = value;
}
}
控制台上打印的输出如下所示。
输出:
参考资料: https: Java Java 函数)