Java 如何将lambda返回到比较方法

Java 如何将lambda返回到比较方法,java,lambda,java-8,comparator,Java,Lambda,Java 8,Comparator,我有一个代码需要修改。 看起来是这样的: new ComparatorFactory<>(thing -> thing.getLong()); return new ComparatorFactory.bySmthThenLong(things); 因此,根据我的理解,这个比较器的创建是这样的: Comparator<T> bySmthThenLong(Collection<T> things) { return bySmth(things).th

我有一个代码需要修改。 看起来是这样的:

new ComparatorFactory<>(thing -> thing.getLong());
return new ComparatorFactory.bySmthThenLong(things);
因此,根据我的理解,这个比较器的创建是这样的:

Comparator<T> bySmthThenLong(Collection<T> things) {
  return bySmth(things).thenComparing(preprocessLong(getLong));
}
比较器bysmthenlong(集合事物){
按mth(things)返回,然后进行比较(preprocessLong(getLong));
}

但是我不明白
prepreprelong
方法内部应该是什么样子。你能帮忙吗?谢谢。

如果您查看JavaDoc中的“函数”接口,您可以看到,使用lambda覆盖的方法是“apply()”方法(因为它是唯一一个抽象的方法) 因此,在新函数中调用apply,如下所示:

private Function<T, Long> preprocessLong(Function<T, Long> getLong) {
   return (thing) -> getLong.apply(thing) / someNumber;
}
私有函数预处理长(函数getLong){
return(thing)->getLong.apply(thing)/someNumber;
}

看起来这很有效,谢谢。如果我有一个条件,所以
返回条件怎么办?(thing)->getLong.apply(thing)/someNumber:0
返回0如果不是真的,这里怎么做?
return(thing)->(condition)?getLong.apply(thing)/someNumber:0谢谢,这很有道理!
Comparator<T> bySmthThenLong(Collection<T> things) {
  return bySmth(things).thenComparing(preprocessLong(getLong));
}
private Function<T, Long> preprocessLong(Function<T, Long> getLong) {
   return (thing) -> getLong.apply(thing) / someNumber;
}