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

The following are Jave code examples for showing how to use nullsLast() 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 5 votes
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");
}
 
Example 2
Project: openjdk-jdk10   File: BasicTest.java   View Source Code Vote up 5 votes
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");
}
 
Example 3
Project: oryx2   File: Pairs.java   View Source Code Vote up 5 votes
/**
 * @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);
}
 
Example 4
Project: oryx2   File: Pairs.java   View Source Code Vote up 5 votes
/**
 * @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);
}