本文整理汇总了Java中java.util.Comparator.nullsFirst方法的典型用法代码示例。如果您正苦于以下问题:Java Comparator.nullsFirst方法的具体用法?Java Comparator.nullsFirst怎么用?Java Comparator.nullsFirst使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Comparator
的用法示例。
在下文中一共展示了Comparator.nullsFirst方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testNullsFirst
import java.util.Comparator; //导入方法依赖的package包/类
public void testNullsFirst() {
Comparator<String> strcmp = Comparator.nullsFirst(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[6], people[5]);
// John.null vs Mary.null - solve by first name
assertComparison(cmp, people[7], people[6]);
// More than one thenComparing
strcmp = Comparator.nullsFirst(Comparator.comparingInt(String::length)
.thenComparing(String.CASE_INSENSITIVE_ORDER));
assertComparison(strcmp, null, "abc");
assertComparison(strcmp, "ab", "abc");
assertComparison(strcmp, "abc", "def");
assertEquals(0, strcmp.compare("abc", "ABC"));
// Ensure reverse still handle null properly
Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder());
assertComparison(strcmp2, "abc", null);
assertComparison(strcmp2, "abc", "ab");
assertComparison(strcmp2, "def", "abc");
assertComparison(strcmp2, "ABC", "abc");
// Considering non-null values to be equal
Comparator<String> blind = Comparator.nullsFirst(null);
assertComparison(blind, null, "abc");
assertEquals(0, blind.compare("abc", "def"));
// reverse still consider non-null values to be equal
strcmp = blind.reversed();
assertComparison(strcmp, "abc", null);
assertEquals(0, strcmp.compare("abc", "def"));
// chain with another comparator to compare non-nulls
strcmp = blind.thenComparing(Comparator.naturalOrder());
assertComparison(strcmp, null, "abc");
assertComparison(strcmp, "abc", "def");
}
示例2: getComparator
import java.util.Comparator; //导入方法依赖的package包/类
/**
* the comparator.
*
* @return the comparator.
*/
public static Comparator<RemoteNodeData> getComparator() {
final Comparator<String> nullSafeStringComparator = Comparator.nullsFirst(String::compareToIgnoreCase);
final Comparator<Long> nullSafeLongComparator = Comparator.nullsFirst(Long::compareTo);
final Comparator<RemoteNodeData> c = Comparator
/** getConnectionPhase */
.comparing(RemoteNodeData::getConnectionPhase)
/** getVersion */
.thenComparing(RemoteNodeData::getVersion, nullSafeStringComparator)
/** getBlockHeight */
.thenComparing(RemoteNodeData::getBlockHeight, nullSafeLongComparator)
/** getTcpAddressAndPortString */
.thenComparing(RemoteNodeData::getTcpAddressAndPortString, nullSafeStringComparator)
/** getLastMessageTimestamp */
.thenComparing(Comparator.comparing(RemoteNodeData::getLastMessageTimestamp, nullSafeLongComparator));
return c;
}