本文整理汇总了Java中java.util.Comparator.comparingDouble方法的典型用法代码示例。如果您正苦于以下问题:Java Comparator.comparingDouble方法的具体用法?Java Comparator.comparingDouble怎么用?Java Comparator.comparingDouble使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Comparator
的用法示例。
在下文中一共展示了Comparator.comparingDouble方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compare
import java.util.Comparator; //导入方法依赖的package包/类
@Override
public int compare(T o1, T o2) {
Comparable obj1 = null;
Comparable obj2 = null;
try {
obj1 = (Comparable) PropertyUtils.getProperty(o1, this.propertyPath);
obj2 = (Comparable) PropertyUtils.getProperty(o2, this.propertyPath);
} catch (NestedNullException ignored) {
// Ignored, als het property NULL is, dan vergelijken met NULL
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new IllegalArgumentException("Could not retrieve property " + this.propertyPath, e);
}
Comparator<Comparable<Object>> objectComparator = null;
if ((obj1 != null && obj2 != null) && obj1 instanceof String) {
obj1 = ((String) obj1).toLowerCase().trim();
obj2 = ((String) obj2).toLowerCase().trim();
if (!StringUtils.isEmpty((String) obj1) && !StringUtils.isEmpty((String) obj2)) {
if (StringUtils.isNumeric((String) obj1) && StringUtils.isNumeric((String) obj2)) {
objectComparator = Comparator.comparingDouble(o -> new Double(String.valueOf(o)));
}
}
}
if (objectComparator == null) {
objectComparator = Comparator.naturalOrder();
}
//noinspection unchecked
return Comparator.nullsLast(objectComparator).compare(obj1, obj2);
}
示例2: getBestGoodsWish
import java.util.Comparator; //导入方法依赖的package包/类
/**
* Gets the best goods wish for a carrier unit.
*
* @param aiUnit The carrier {@code AIUnit}.
* @param goodsType The {@code GoodsType} to wish for.
* @return The best {@code GoodsWish} for the unit.
*/
public GoodsWish getBestGoodsWish(AIUnit aiUnit, GoodsType goodsType) {
final Unit carrier = aiUnit.getUnit();
final ToDoubleFunction<GoodsWish> wishValue
= cacheDouble(gw -> {
int turns = carrier.getTurnsToReach(carrier.getLocation(),
gw.getDestination());
return (turns >= Unit.MANY_TURNS) ? -1.0
: (double)gw.getValue() / turns;
});
final Comparator<GoodsWish> comp
= Comparator.comparingDouble(wishValue);
List<GoodsWish> wishes = goodsWishes.get(goodsType);
return (wishes == null) ? null
: maximize(wishes, gw -> wishValue.applyAsDouble(gw) > 0.0, comp);
}
示例3: testDoubleComparator
import java.util.Comparator; //导入方法依赖的package包/类
public void testDoubleComparator() {
Thing[] things = new Thing[doubleValues.length];
for (int i=0; i<doubleValues.length; i++)
things[i] = new Thing(0, 0L, doubleValues[i], null);
Comparator<Thing> comp = Comparator.comparingDouble(new ToDoubleFunction<Thing>() {
@Override
public double applyAsDouble(Thing thing) {
return thing.getDoubleField();
}
});
assertComparisons(things, comp, comparisons);
}
示例4: comparator
import java.util.Comparator; //导入方法依赖的package包/类
/** */
private static Comparator<IgniteBiTuple<Integer, IgniteBiTuple<Integer, Double>>> comparator() {
return Comparator.comparingDouble(bt -> bt != null && bt.get2() != null ? bt.get2().get2() : Double.NEGATIVE_INFINITY);
}
示例5: cachingDoubleComparator
import java.util.Comparator; //导入方法依赖的package包/类
/**
* Helper to create a caching comparator.
*
* @param <T> The argument type to be converted to double.
* @param f The double valued function to use in comparison.
* @return A caching {@code Comparator}.
*/
public static <T> Comparator<T> cachingDoubleComparator(Function<T, Double> f) {
return Comparator.comparingDouble(cacheDouble(f));
}