本文整理汇总了Java中java.util.Comparator.reversed方法的典型用法代码示例。如果您正苦于以下问题:Java Comparator.reversed方法的具体用法?Java Comparator.reversed怎么用?Java Comparator.reversed使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Comparator
的用法示例。
在下文中一共展示了Comparator.reversed方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: getRevisions
import java.util.Comparator; //导入方法依赖的package包/类
public List<Revision> getRevisions(String id, Params params) {
AuditReader reader = getAuditReader();
List<Number> revisionNumbers = reader.getRevisions(type, id);
Map<Number, Revision> revisionMap = reader.findRevisions(Revision.class, asSet(revisionNumbers));
Collection<Revision> revisions = revisionMap.values();
Comparator<Revision> timestampComparator = Comparator.comparingLong(Revision::getTimestamp);
Comparator<Revision> authorComparator = Comparator.comparing(Revision::getAuthor);
Comparator<Revision> comparator;
if (AUTHOR.equals(params.getSort())) {
comparator = authorComparator;
} else {
comparator = timestampComparator;
}
if (params.getOrder() == Order.DESC) {
comparator = comparator.reversed();
}
return Ordering.from(comparator)
.sortedCopy(revisions);
}
示例3: resolveExistingMigrations
import java.util.Comparator; //导入方法依赖的package包/类
static List<Path> resolveExistingMigrations(File migrationsDir, boolean reversed, boolean onlySchemaMigrations) {
if (!migrationsDir.exists()) {
migrationsDir.mkdirs();
}
File[] files = migrationsDir.listFiles();
if (files == null) {
return Collections.emptyList();
}
Comparator<Path> pathComparator = Comparator.comparingInt(FileResolver::compareVersionedMigrations);
if (reversed) {
pathComparator = pathComparator.reversed();
}
return Arrays.stream(files)
.map(File::toPath)
.filter(path -> !onlySchemaMigrations || SCHEMA_FILENAME_PATTERN.matcher(path.getFileName().toString()).matches())
.sorted(pathComparator)
.collect(Collectors.toList());
}
示例4: instanceMethodReferenceOfArbitraryObject
import java.util.Comparator; //导入方法依赖的package包/类
private static void instanceMethodReferenceOfArbitraryObject() {
System.err.println("Instance method reference of ArbitraryObject");
Comparator<String> stringIgnoreCase = String::compareToIgnoreCase;
// this is equivalent to below
Comparator<String> stringIgnoreCase2 = (first, second) -> first
.compareToIgnoreCase(second);
Comparator<String> reverseOrder = stringIgnoreCase.reversed();
List<String> values = Arrays.asList("Gaurav", "Sunil", "Anil", "Rajesh", "sajjad");
System.out.println(values);
Collections.sort(values, stringIgnoreCase);
System.out.println(values);
}
示例5: testReverseComparator
import java.util.Comparator; //导入方法依赖的package包/类
public void testReverseComparator() {
Comparator<String> cmpr = Comparator.reverseOrder();
Comparator<String> cmp = cmpr.reversed();
assertEquals(cmp.reversed(), cmpr);
assertEquals(0, cmp.compare("a", "a"));
assertEquals(0, cmpr.compare("a", "a"));
assertTrue(cmp.compare("a", "b") < 0);
assertTrue(cmpr.compare("a", "b") > 0);
assertTrue(cmp.compare("b", "a") > 0);
assertTrue(cmpr.compare("b", "a") < 0);
}
示例6: testReverseComparator2
import java.util.Comparator; //导入方法依赖的package包/类
public void testReverseComparator2() {
Comparator<String> cmp = (s1, s2) -> s1.length() - s2.length();
Comparator<String> cmpr = cmp.reversed();
assertEquals(cmpr.reversed(), cmp);
assertEquals(0, cmp.compare("abc", "def"));
assertEquals(0, cmpr.compare("abc", "def"));
assertTrue(cmp.compare("abcd", "def") > 0);
assertTrue(cmpr.compare("abcd", "def") < 0);
assertTrue(cmp.compare("abc", "defg") < 0);
assertTrue(cmpr.compare("abc", "defg") > 0);
}
示例7: 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");
}
示例8: orderByFirst
import java.util.Comparator; //导入方法依赖的package包/类
/**
* @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);
}
示例9: orderBySecond
import java.util.Comparator; //导入方法依赖的package包/类
/**
* @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);
}
示例10: forSortDirective
import java.util.Comparator; //导入方法依赖的package包/类
public static <T> Comparator<T> forSortDirective(RestSortDirective sortDirective, Class<T> rootClass) {
Comparator<T> comparator = new XmlElementPathBeanComparator<>(sortDirective.getProperty(), rootClass);
return (sortDirective.isDescending() ? comparator.reversed() : comparator);
}
示例11: apply
import java.util.Comparator; //导入方法依赖的package包/类
@Override
public Comparator<HealthResponse> apply(Order order) {
Comparator<HealthResponse> comparator = comparators.getOrDefault(order.getProperty(), SORT_BY_SERVICE);
return order.getDirection().isDescending() ? comparator.reversed() : comparator;
}