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