java.util.Comparator.naturalOrder()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(223)

本文整理了Java中java.util.Comparator.naturalOrder()方法的一些代码示例,展示了Comparator.naturalOrder()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Comparator.naturalOrder()方法的具体详情如下:
包路径:java.util.Comparator
类名称:Comparator
方法名:naturalOrder

Comparator.naturalOrder介绍

暂无

代码示例

代码示例来源:origin: apache/incubator-druid

@Override
public Comparator<Integer> getComparator()
{
 return Comparator.naturalOrder();
}

代码示例来源:origin: apache/incubator-druid

@Override
public Comparator<Double> getComparator()
{
 return Comparator.naturalOrder();
}

代码示例来源:origin: apache/incubator-druid

@Override
 public Comparator<Integer> getComparator()
 {
  return Comparator.naturalOrder();
 }
}

代码示例来源:origin: apache/incubator-druid

@Override
public Comparator<String> getComparator()
{
 return Comparator.nullsFirst(Comparator.naturalOrder());
}

代码示例来源:origin: dropwizard/dropwizard

@Override
  public int compare(EndpointLogLine endpointA, EndpointLogLine endpointB) {
    return Comparator.<EndpointLogLine, String>comparing(endpoint -> endpoint.basePath)
        .thenComparing(endpoint -> endpoint.httpMethod, Comparator.nullsLast(Comparator.naturalOrder()))
        .compare(endpointA, endpointB);
  }
}

代码示例来源:origin: vavr-io/vavr

/**
 * Converts this to a {@link Map}.
 *
 * @param f   A function that maps an element to a key/value pair represented by Tuple2
 * @param <K> The key type
 * @param <V> The value type
 * @return A new {@link TreeMap}.
 */
default <K extends Comparable<? super K>, V> SortedMap<K, V> toSortedMap(Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) {
  Objects.requireNonNull(f, "f is null");
  return toSortedMap(Comparator.naturalOrder(), f);
}

代码示例来源:origin: SonarSource/sonarqube

private static List<Long> selectVersions(Connection connection) throws SQLException {
  try (Statement statement = connection.createStatement();
   ResultSet resultSet = statement.executeQuery("select version from " + SCHEMA_MIGRATIONS_TABLE)) {
   List<Long> res = new ArrayList<>();
   while (resultSet.next()) {
    res.add(resultSet.getLong(1));
   }
   return res.stream()
    .sorted(Comparator.naturalOrder())
    .collect(toList());
  }
 }
}

代码示例来源:origin: graphql-java/graphql-java

private <T, U extends Comparable<? super U>> Comparator<T> comparing(
    Function<? super T, ? extends U> keyExtractor) {
  return Comparator.comparing(keyExtractor, nullsLast(naturalOrder()));
}

代码示例来源:origin: prestodb/presto

public MergingPageIterator(
    Collection<Iterator<Page>> iterators,
    List<Type> types,
    List<Integer> sortFields,
    List<SortOrder> sortOrders)
{
  requireNonNull(sortFields, "sortFields is null");
  requireNonNull(sortOrders, "sortOrders is null");
  checkArgument(sortFields.size() == sortOrders.size(), "sortFields and sortOrders size must match");
  this.types = ImmutableList.copyOf(requireNonNull(types, "types is null"));
  this.sortFields = ImmutableList.copyOf(sortFields);
  this.sortOrders = ImmutableList.copyOf(sortOrders);
  this.pageBuilder = new PageBuilder(types);
  this.pagePositions = mergeSorted(
      iterators.stream()
          .map(pages -> concat(transform(pages, PagePositionIterator::new)))
          .collect(toList()),
      naturalOrder());
}

代码示例来源:origin: google/error-prone

private static String formatLockString(Set<GuardedByExpression> locks) {
 ImmutableList<String> sortedUnhandled =
   FluentIterable.from(locks)
     .transform(Functions.toStringFunction())
     .toSortedList(Comparator.naturalOrder());
 return Joiner.on(", ").join(sortedUnhandled);
}

代码示例来源:origin: SonarSource/sonarqube

@Override
public Optional<ReadError> read(DbFileSources.Line.Builder lineBuilder) {
 Predicate<Map.Entry<TextBlock, Integer>> containsLine = new TextBlockContainsLine(lineBuilder.getLine());
 // list is sorted to cope with the non-guaranteed order of Map entries which would trigger false detection of changes
 // in {@link DbFileSources.Line#getDuplicationList()}
 duplicatedTextBlockIndexByTextBlock.entrySet().stream()
  .filter(containsLine)
  .map(Map.Entry::getValue)
  .sorted(Comparator.naturalOrder())
  .forEach(lineBuilder::addDuplication);
 return Optional.empty();
}

代码示例来源:origin: google/guava

Comparator<? super E> comparator = spliterator.getComparator();
if (comparator == null) {
 comparator = (Comparator) Comparator.naturalOrder();

代码示例来源:origin: google/guava

public void testEmptiesFirst() {
 Optional<String> empty = Optional.empty();
 Optional<String> abc = Optional.of("abc");
 Optional<String> z = Optional.of("z");
 Comparator<Optional<String>> comparator = Comparators.emptiesFirst(comparing(String::length));
 Helpers.testComparator(comparator, empty, z, abc);
 // Just demonstrate that no explicit type parameter is required
 comparator = Comparators.emptiesFirst(naturalOrder());
}

代码示例来源:origin: google/guava

public void testEmptiesLast() {
  Optional<String> empty = Optional.empty();
  Optional<String> abc = Optional.of("abc");
  Optional<String> z = Optional.of("z");

  Comparator<Optional<String>> comparator = Comparators.emptiesLast(comparing(String::length));
  Helpers.testComparator(comparator, z, abc, empty);

  // Just demonstrate that no explicit type parameter is required
  comparator = Comparators.emptiesLast(naturalOrder());
 }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testStringArrayToResourceArray() {
  conversionService.addConverter(new MyStringArrayToResourceArrayConverter());
  Resource[] converted = conversionService.convert(new String[] { "x1", "z3" }, Resource[].class);
  List<String> descriptions = Arrays.stream(converted).map(Resource::getDescription).sorted(naturalOrder()).collect(toList());
  assertEquals(Arrays.asList("1", "3"), descriptions);
}

代码示例来源:origin: google/guava

public void testGreatestCollector() {
 CollectorTester.of(Comparators.<Integer>greatest(2, Comparator.naturalOrder()))
   .expectCollects(Arrays.asList(6, 5), 1, 2, 3, 4, 5, 6)
   .expectCollects(Arrays.asList(1), 1)
   .expectCollects(Collections.emptyList());
}

代码示例来源:origin: google/guava

public void testLeastCollector() {
 CollectorTester.of(Comparators.<Integer>least(2, Comparator.naturalOrder()))
   .expectCollects(Arrays.asList(1, 2), 1, 2, 3, 4, 5, 6)
   .expectCollects(Arrays.asList(1), 1)
   .expectCollects(Collections.emptyList());
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "snapshot")
public void snapshot(boolean ascending, int limit, long nanos, Function<Long, Long> transformer) {
 int count = 21;
 timerWheel.nanos = nanos;
 int expected = Math.min(limit, count);
 Comparator<Long> order = ascending ? Comparator.naturalOrder() : Comparator.reverseOrder();
 List<Long> times = IntStream.range(0, count).mapToLong(i -> {
  long time = nanos + TimeUnit.SECONDS.toNanos(2 << i);
  timerWheel.schedule(new Timer(time));
  return time;
 }).boxed().sorted(order).collect(toList()).subList(0, expected);
 when(transformer.apply(anyLong())).thenAnswer(invocation -> invocation.getArgument(0));
 assertThat(snapshot(ascending, limit, transformer), is(times));
 verify(transformer, times(expected)).apply(anyLong());
}

代码示例来源:origin: apache/hive

@Override
 public int compare(Operator<?> o1, Operator<?> o2) {
  Long id1 = Long.valueOf(o1.getIdentifier());
  Long id2 = Long.valueOf(o2.getIdentifier());
  int c0 = Objects.compare(o1.getOperatorName(), o2.getOperatorName(), Comparator.naturalOrder());
  if (c0 != 0) {
   return c0;
  }
  return Long.compare(id1, id2);
 }
};

代码示例来源:origin: google/guava

public void testToImmutableSortedMapMerging() {
  Collector<Entry<String, Integer>, ?, ImmutableSortedMap<String, Integer>> collector =
    ImmutableSortedMap.toImmutableSortedMap(
      Comparator.naturalOrder(), Entry::getKey, Entry::getValue, Integer::sum);
  Equivalence<ImmutableMap<String, Integer>> equivalence =
    Equivalence.equals()
      .<Entry<String, Integer>>pairwise()
      .onResultOf(ImmutableMap::entrySet);
  CollectorTester.of(collector, equivalence)
    .expectCollects(
      ImmutableSortedMap.of("one", 1, "three", 3, "two", 4),
      mapEntry("one", 1),
      mapEntry("two", 2),
      mapEntry("three", 3),
      mapEntry("two", 2));
 }
}

相关文章