当前位置: 首页>>代码示例>>Java>>正文


Java Comparator.thenComparing方法代码示例

本文整理汇总了Java中java.util.Comparator.thenComparing方法的典型用法代码示例。如果您正苦于以下问题:Java Comparator.thenComparing方法的具体用法?Java Comparator.thenComparing怎么用?Java Comparator.thenComparing使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.Comparator的用法示例。


在下文中一共展示了Comparator.thenComparing方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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");
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:38,代码来源:BasicTest.java

示例2: main

import java.util.Comparator; //导入方法依赖的package包/类
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));
}
 
开发者ID:gauravrmazra,项目名称:gauravbytes,代码行数:20,代码来源:ComparatorExample.java

示例3: findStudent

import java.util.Comparator; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:28,代码来源:Colony.java

示例4: compare

import java.util.Comparator; //导入方法依赖的package包/类
@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);
}
 
开发者ID:spring-io,项目名称:artifactory-resource,代码行数:10,代码来源:FileComparator.java

示例5: testKVComparators

import java.util.Comparator; //导入方法依赖的package包/类
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));

}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:14,代码来源:EntryComparators.java

示例6: testNullsLast

import java.util.Comparator; //导入方法依赖的package包/类
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");
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:37,代码来源:BasicTest.java

示例7: testComposeComparator

import java.util.Comparator; //导入方法依赖的package包/类
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);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:13,代码来源:BasicTest.java

示例8: assertPairComparison

import java.util.Comparator; //导入方法依赖的package包/类
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);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:43,代码来源:EntryComparators.java


注:本文中的java.util.Comparator.thenComparing方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。