本文整理匯總了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));
}