本文整理汇总了Java中java.util.Comparator.nullsLast方法的典型用法代码示例。如果您正苦于以下问题:Java Comparator.nullsLast方法的具体用法?Java Comparator.nullsLast怎么用?Java Comparator.nullsLast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Comparator
的用法示例。
在下文中一共展示了Comparator.nullsLast方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testNullsLast
import java.util.Comparator; //导入方法依赖的package包/类
public void testNullsLast() {
Comparator<String> strcmp = Comparator.nullsLast(Comparator.naturalOrder());
Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp)
.thenComparing(People::getFirstName, strcmp);
// Mary.null vs Mary.Cook - solve by last name
assertComparison(cmp, people[5], people[6]);
// John.null vs Mary.null - solve by first name
assertComparison(cmp, people[7], people[6]);
// More than one thenComparing
strcmp = Comparator.nullsLast(Comparator.comparingInt(String::length)
.thenComparing(String.CASE_INSENSITIVE_ORDER));
assertComparison(strcmp, "abc", null);
assertComparison(strcmp, "ab", "abc");
assertComparison(strcmp, "abc", "def");
// Ensure reverse still handle null properly
Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder());
assertComparison(strcmp2, null, "abc");
assertComparison(strcmp2, "abc", "ab");
assertComparison(strcmp2, "def", "abc");
assertComparison(strcmp2, "ABC", "abc");
// Considering non-null values to be equal
Comparator<String> blind = Comparator.nullsLast(null);
assertComparison(blind, "abc", null);
assertEquals(0, blind.compare("abc", "def"));
// reverse still consider non-null values to be equal
strcmp = blind.reversed();
assertComparison(strcmp, null, "abc");
assertEquals(0, strcmp.compare("abc", "def"));
// chain with another comparator to compare non-nulls
strcmp = blind.thenComparing(Comparator.naturalOrder());
assertComparison(strcmp, "abc", null);
assertComparison(strcmp, "abc", "def");
}
示例2: orderByFirst
import java.util.Comparator; //导入方法依赖的package包/类
/**
* @param order whether to sort ascending or descending; {@code null} comes last
* @param <C> type of first element in {@link Pair}s to be compared
* @param <D> type of second element in {@link Pair}s to be compared
* @return an ordering on {@link Pair}s by first element as a {@link Comparator}
*/
public static <C extends Comparable<C>,D> Comparator<Pair<C,D>> orderByFirst(SortOrder order) {
Comparator<Pair<C,D>> ordering = Comparator.comparing(Pair::getFirst);
if (order == SortOrder.DESCENDING) {
ordering = ordering.reversed();
}
return Comparator.nullsLast(ordering);
}
示例3: orderBySecond
import java.util.Comparator; //导入方法依赖的package包/类
/**
* @param order whether to sort ascending or descending; {@code null} comes last
* @param <C> type of first element in {@link Pair}s to be compared
* @param <D> type of second element in {@link Pair}s to be compared
* @return an ordering on {@link Pair}s by second element as a {@link Comparator}
*/
public static <C,D extends Comparable<D>> Comparator<Pair<C,D>> orderBySecond(SortOrder order) {
Comparator<Pair<C,D>> ordering = Comparator.comparing(Pair::getSecond);
if (order == SortOrder.DESCENDING) {
ordering = ordering.reversed();
}
return Comparator.nullsLast(ordering);
}