Java Code Examples for java.util.Comparator.nullsFirst()

The following are Jave code examples for showing how to use nullsFirst() of the java.util.Comparator class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: jdk8u-jdk   File: BasicTest.java   View Source Code Vote up 9 votes
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");
}
 
Example 2
Project: neo-java   File: RemoteNodeData.java   View Source Code Vote up 6 votes
/**
 * 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;
}
 
Example 3
Project: openjdk-jdk10   File: BasicTest.java   View Source Code Vote up 5 votes
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");
}