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

The following are Jave code examples for showing how to use thenComparing() 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: gauravbytes   File: ComparatorExample.java   View Source Code Vote up 7 votes
public static void main(String[] args) {
	Comparator<Person> byAge = (p1, p2) -> {
		return Integer.compare(p1.getAge(), p2.getAge());
	};

	Comparator<Person> byFirstName = (p1, p2) -> {
		return p1.getFirstName().compareTo(p2.getFirstName());
	};

	Comparator<Person> byAgeThenFirstName = byAge.thenComparing(byFirstName);

	Calculator sum = (a, b) -> Integer.sum(a, b);
	Calculator multiply = (a, b) -> a * b;
	Calculator divide = (a, b) -> a / b;

	System.out.println(sum.calculate(10, 20));
	System.out.println(multiply.calculate(10, 2));
	System.out.println(divide.calculate(20, 2));
}
 
Example 3
Project: freecol   File: Colony.java   View Source Code Vote up 6 votes
/**
 * Find a student for the specified teacher.
 * Do not search if ALLOW_STUDENT_SELECTION is true--- its the
 * player's job then.
 *
 * @param teacher The teacher {@code Unit} that needs a student.
 * @return A potential student, or null of none found.
 */
public Unit findStudent(final Unit teacher) {
    if (getSpecification().getBoolean(GameOptions.ALLOW_STUDENT_SELECTION))
        return null; // No automatic assignment
    final GoodsType expertProduction
            = teacher.getType().getExpertProduction();
    final Predicate<Unit> teacherPred = u ->
            u.getTeacher() == null && u.canBeStudent(teacher);
    // Always pick the student with the least skill first.
    // Break ties by favouring the one working in the teacher's trade,
    // otherwise first applicant wins.
    final Comparator<Unit> skillComparator
            = Comparator.comparingInt(Unit::getSkillLevel);
    final Comparator<Unit> tradeComparator
            = Comparator.comparingInt(u ->
            (u.getWorkType() == expertProduction) ? 0 : 1);
    final Comparator<Unit> fullComparator
            = skillComparator.thenComparing(tradeComparator);
    return minimize(getUnits(), teacherPred, fullComparator);
}
 
Example 4
Project: FreeCol   File: Colony.java   View Source Code Vote up 6 votes
/**
 * Find a student for the specified teacher.
 * Do not search if ALLOW_STUDENT_SELECTION is true--- its the
 * player's job then.
 *
 * @param teacher The teacher {@code Unit} that needs a student.
 * @return A potential student, or null of none found.
 */
public Unit findStudent(final Unit teacher) {
    if (getSpecification().getBoolean(GameOptions.ALLOW_STUDENT_SELECTION))
        return null; // No automatic assignment
    final GoodsType expertProduction
            = teacher.getType().getExpertProduction();
    final Predicate<Unit> teacherPred = u ->
            u.getTeacher() == null && u.canBeStudent(teacher);
    // Always pick the student with the least skill first.
    // Break ties by favouring the one working in the teacher's trade,
    // otherwise first applicant wins.
    final Comparator<Unit> skillComparator
            = Comparator.comparingInt(Unit::getSkillLevel);
    final Comparator<Unit> tradeComparator
            = Comparator.comparingInt(u ->
            (u.getWorkType() == expertProduction) ? 0 : 1);
    final Comparator<Unit> fullComparator
            = skillComparator.thenComparing(tradeComparator);
    return minimize(getUnits(), teacherPred, fullComparator);
}
 
Example 5
Project: artifactory-resource   File: FileComparator.java   View Source Code Vote up 5 votes
@Override
public int compare(File file1, File file2) {
	Comparator<File> comparator = Comparator.comparing(File::getParentFile);
	comparator = comparator.thenComparingInt(this::byFileType);
	comparator = comparator.thenComparingInt(this::byPomExtension);
	comparator = comparator.thenComparing(this::getFileExtension);
	comparator = comparator.thenComparing(FileComparator::getNameWithoutExtension);
	return comparator.compare(file1, file2);
}
 
Example 6
Project: jdk8u-jdk   File: EntryComparators.java   View Source Code Vote up 5 votes
public void testKVComparators() {
    // Comparator<People> cmp = Comparator.naturalOrder(); // Should fail to compiler as People is not comparable
    // We can use simple comparator, but those have been tested above.
    // Thus choose to do compose for some level of interation.
    Comparator<People> cmp1 = Comparator.comparing(People::getFirstName);
    Comparator<People> cmp2 = Comparator.comparing(People::getLastName);
    Comparator<People> cmp = cmp1.thenComparing(cmp2);

    assertPairComparison(people[0], people[0], people[1], people[1],
                     Map.Entry.<People, People>comparingByKey(cmp),
                     Map.Entry.<People, People>comparingByValue(cmp));

}
 
Example 7
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 8
Project: jdk8u-jdk   File: BasicTest.java   View Source Code Vote up 5 votes
public void testComposeComparator() {
    // Longer string in front
    Comparator<String> first = (s1, s2) -> s2.length() - s1.length();
    Comparator<String> second = Comparator.naturalOrder();
    Comparator<String> composed = first.thenComparing(second);

    assertTrue(composed.compare("abcdefg", "abcdef") < 0);
    assertTrue(composed.compare("abcdef", "abcdefg") > 0);
    assertTrue(composed.compare("abcdef", "abcdef") == 0);
    assertTrue(composed.compare("abcdef", "ghijkl") < 0);
    assertTrue(composed.compare("ghijkl", "abcdefg") > 0);
}
 
Example 9
Project: openjdk-jdk10   File: EntryComparators.java   View Source Code Vote up 5 votes
public void testKVComparators() {
    // Comparator<People> cmp = Comparator.naturalOrder(); // Should fail to compiler as People is not comparable
    // We can use simple comparator, but those have been tested above.
    // Thus choose to do compose for some level of interation.
    Comparator<People> cmp1 = Comparator.comparing(People::getFirstName);
    Comparator<People> cmp2 = Comparator.comparing(People::getLastName);
    Comparator<People> cmp = cmp1.thenComparing(cmp2);

    assertPairComparison(people[0], people[0], people[1], people[1],
                     Map.Entry.<People, People>comparingByKey(cmp),
                     Map.Entry.<People, People>comparingByValue(cmp));

}
 
Example 10
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");
}
 
Example 11
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 12
Project: openjdk-jdk10   File: BasicTest.java   View Source Code Vote up 5 votes
public void testComposeComparator() {
    // Longer string in front
    Comparator<String> first = (s1, s2) -> s2.length() - s1.length();
    Comparator<String> second = Comparator.naturalOrder();
    Comparator<String> composed = first.thenComparing(second);

    assertTrue(composed.compare("abcdefg", "abcdef") < 0);
    assertTrue(composed.compare("abcdef", "abcdefg") > 0);
    assertTrue(composed.compare("abcdef", "abcdef") == 0);
    assertTrue(composed.compare("abcdef", "ghijkl") < 0);
    assertTrue(composed.compare("ghijkl", "abcdefg") > 0);
}
 
Example 13
Project: jdk8u-jdk   File: EntryComparators.java   View Source Code Vote up 4 votes
private <K, V> void assertPairComparison(K k1, V v1, K k2, V v2,
                                    Comparator<Map.Entry<K, V>> ck,
                                    Comparator<Map.Entry<K, V>> cv) {
    final Map.Entry<K, V> p11 = new AbstractMap.SimpleImmutableEntry<>(k1, v1);
    final Map.Entry<K, V> p12 = new AbstractMap.SimpleImmutableEntry<>(k1, v2);
    final Map.Entry<K, V> p21 = new AbstractMap.SimpleImmutableEntry<>(k2, v1);
    final Map.Entry<K, V> p22 = new AbstractMap.SimpleImmutableEntry<>(k2, v2);

    assertTrue(ck.compare(p11, p11) == 0);
    assertTrue(ck.compare(p12, p11) == 0);
    assertTrue(ck.compare(p11, p12) == 0);
    assertTrue(ck.compare(p12, p22) < 0);
    assertTrue(ck.compare(p12, p21) < 0);
    assertTrue(ck.compare(p21, p11) > 0);
    assertTrue(ck.compare(p21, p12) > 0);

    assertTrue(cv.compare(p11, p11) == 0);
    assertTrue(cv.compare(p12, p11) > 0);
    assertTrue(cv.compare(p11, p12) < 0);
    assertTrue(cv.compare(p12, p22) == 0);
    assertTrue(cv.compare(p12, p21) > 0);
    assertTrue(cv.compare(p21, p11) == 0);
    assertTrue(cv.compare(p21, p12) < 0);

    Comparator<Map.Entry<K, V>> cmp = ck.thenComparing(cv);
    assertTrue(cmp.compare(p11, p11) == 0);
    assertTrue(cmp.compare(p12, p11) > 0);
    assertTrue(cmp.compare(p11, p12) < 0);
    assertTrue(cmp.compare(p12, p22) < 0);
    assertTrue(cmp.compare(p12, p21) < 0);
    assertTrue(cmp.compare(p21, p11) > 0);
    assertTrue(cmp.compare(p21, p12) > 0);

    cmp = cv.thenComparing(ck);
    assertTrue(cmp.compare(p11, p11) == 0);
    assertTrue(cmp.compare(p12, p11) > 0);
    assertTrue(cmp.compare(p11, p12) < 0);
    assertTrue(cmp.compare(p12, p22) < 0);
    assertTrue(cmp.compare(p12, p21) > 0);
    assertTrue(cmp.compare(p21, p11) > 0);
    assertTrue(cmp.compare(p21, p12) < 0);
}
 
Example 14
Project: openjdk-jdk10   File: EntryComparators.java   View Source Code Vote up 4 votes
private <K, V> void assertPairComparison(K k1, V v1, K k2, V v2,
                                    Comparator<Map.Entry<K, V>> ck,
                                    Comparator<Map.Entry<K, V>> cv) {
    final Map.Entry<K, V> p11 = new AbstractMap.SimpleImmutableEntry<>(k1, v1);
    final Map.Entry<K, V> p12 = new AbstractMap.SimpleImmutableEntry<>(k1, v2);
    final Map.Entry<K, V> p21 = new AbstractMap.SimpleImmutableEntry<>(k2, v1);
    final Map.Entry<K, V> p22 = new AbstractMap.SimpleImmutableEntry<>(k2, v2);

    assertTrue(ck.compare(p11, p11) == 0);
    assertTrue(ck.compare(p12, p11) == 0);
    assertTrue(ck.compare(p11, p12) == 0);
    assertTrue(ck.compare(p12, p22) < 0);
    assertTrue(ck.compare(p12, p21) < 0);
    assertTrue(ck.compare(p21, p11) > 0);
    assertTrue(ck.compare(p21, p12) > 0);

    assertTrue(cv.compare(p11, p11) == 0);
    assertTrue(cv.compare(p12, p11) > 0);
    assertTrue(cv.compare(p11, p12) < 0);
    assertTrue(cv.compare(p12, p22) == 0);
    assertTrue(cv.compare(p12, p21) > 0);
    assertTrue(cv.compare(p21, p11) == 0);
    assertTrue(cv.compare(p21, p12) < 0);

    Comparator<Map.Entry<K, V>> cmp = ck.thenComparing(cv);
    assertTrue(cmp.compare(p11, p11) == 0);
    assertTrue(cmp.compare(p12, p11) > 0);
    assertTrue(cmp.compare(p11, p12) < 0);
    assertTrue(cmp.compare(p12, p22) < 0);
    assertTrue(cmp.compare(p12, p21) < 0);
    assertTrue(cmp.compare(p21, p11) > 0);
    assertTrue(cmp.compare(p21, p12) > 0);

    cmp = cv.thenComparing(ck);
    assertTrue(cmp.compare(p11, p11) == 0);
    assertTrue(cmp.compare(p12, p11) > 0);
    assertTrue(cmp.compare(p11, p12) < 0);
    assertTrue(cmp.compare(p12, p22) < 0);
    assertTrue(cmp.compare(p12, p21) > 0);
    assertTrue(cmp.compare(p21, p11) > 0);
    assertTrue(cmp.compare(p21, p12) < 0);
}