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

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

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

Comparator.nullsFirst介绍

暂无

代码示例

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

/**
 * Returns a comparator of {@link Optional} values which treats {@link Optional#empty} as less
 * than all other values, and orders the rest using {@code valueComparator} on the contained
 * value.
 *
 * @since 22.0
 */
@Beta
public static <T> Comparator<Optional<T>> emptiesFirst(Comparator<? super T> valueComparator) {
 checkNotNull(valueComparator);
 return Comparator.comparing(o -> o.orElse(null), Comparator.nullsFirst(valueComparator));
}

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

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

代码示例来源:origin: allure-framework/allure2

public static Comparator<TestResult> comparingByTimeAsc() {
  return comparing(
      TestResult::getTime,
      nullsFirst(comparing(Time::getStart, nullsFirst(naturalOrder())))
  );
}

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

private Ordering<Row> dimensionOrdering(final String dimension, final StringComparator comparator)
{
 return Ordering.from(
   Comparator.comparing((Row row) -> getDimensionValue(row, dimension), Comparator.nullsFirst(comparator))
 );
}

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

@Override
public Comparator getComparator(
  final List<AggregatorFactory> aggregatorSpecs,
  final List<PostAggregator> postAggregatorSpecs
)
{
 return Comparator.nullsFirst(delegate.getComparator(aggregatorSpecs, postAggregatorSpecs).reversed());
}

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

/**
 * Returns a comparator of {@link Optional} values which treats {@link Optional#empty} as less
 * than all other values, and orders the rest using {@code valueComparator} on the contained
 * value.
 *
 * @since 22.0
 */
@Beta
public static <T> Comparator<Optional<T>> emptiesFirst(Comparator<? super T> valueComparator) {
 checkNotNull(valueComparator);
 return Comparator.comparing(o -> o.orElse(null), Comparator.nullsFirst(valueComparator));
}

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

/**
 * Returns a comparator of {@link Optional} values which treats {@link Optional#empty} as less
 * than all other values, and orders the rest using {@code valueComparator} on the contained
 * value.
 *
 * @since 22.0
 */
@Beta
public static <T> Comparator<Optional<T>> emptiesFirst(Comparator<? super T> valueComparator) {
 checkNotNull(valueComparator);
 return Comparator.comparing(o -> o.orElse(null), Comparator.nullsFirst(valueComparator));
}

代码示例来源:origin: allure-framework/allure2

private static Optional<ExecutorInfo> extractLatestExecutor(final List<LaunchResults> launches) {
  final Comparator<ExecutorInfo> comparator = comparing(ExecutorInfo::getBuildOrder, nullsFirst(naturalOrder()));
  return launches.stream()
      .map(launch -> launch.getExtra(EXECUTORS_BLOCK_NAME))
      .filter(Optional::isPresent)
      .map(Optional::get)
      .filter(ExecutorInfo.class::isInstance)
      .map(ExecutorInfo.class::cast)
      .max(comparator);
}

代码示例来源:origin: allure-framework/allure2

private static Optional<ExecutorInfo> extractLatestExecutor(final List<LaunchResults> launches) {
  final Comparator<ExecutorInfo> comparator = comparing(ExecutorInfo::getBuildOrder, nullsFirst(naturalOrder()));
  return launches.stream()
      .map(launch -> launch.getExtra(EXECUTORS_BLOCK_NAME))
      .filter(Optional::isPresent)
      .map(Optional::get)
      .filter(ExecutorInfo.class::isInstance)
      .map(ExecutorInfo.class::cast)
      .max(comparator);
}

代码示例来源:origin: allure-framework/allure2

private List<Parameter> getParameters(final TestResult result) {
  final TreeSet<Parameter> parametersSet = new TreeSet<>(
      comparing(Parameter::getName, nullsFirst(naturalOrder()))
          .thenComparing(Parameter::getValue, nullsFirst(naturalOrder()))
  );
  parametersSet.addAll(convert(result.getParameters(), this::convert));
  return new ArrayList<>(parametersSet);
}

代码示例来源:origin: allure-framework/allure2

private static Optional<ExecutorInfo> extractLatestExecutor(final List<LaunchResults> launches) {
  final Comparator<ExecutorInfo> comparator = comparing(ExecutorInfo::getBuildOrder, nullsFirst(naturalOrder()));
  return launches.stream()
      .map(launch -> launch.getExtra(EXECUTORS_BLOCK_NAME))
      .filter(Optional::isPresent)
      .map(Optional::get)
      .filter(ExecutorInfo.class::isInstance)
      .map(ExecutorInfo.class::cast)
      .max(comparator);
}

代码示例来源:origin: allure-framework/allure2

private List<Parameter> getParameters(final TestCaseResult source) {
  final TreeSet<Parameter> parametersSet = new TreeSet<>(
      comparing(Parameter::getName, nullsFirst(naturalOrder()))
          .thenComparing(Parameter::getValue, nullsFirst(naturalOrder()))
  );
  parametersSet.addAll(convert(source.getParameters(), this::hasArgumentType, this::convert));
  return new ArrayList<>(parametersSet);
}

代码示例来源:origin: allure-framework/allure2

private static Optional<ExecutorInfo> extractLatestExecutor(final List<LaunchResults> launches) {
  final Comparator<ExecutorInfo> comparator = comparing(ExecutorInfo::getBuildOrder, nullsFirst(naturalOrder()));
  return launches.stream()
      .map(launch -> launch.getExtra(EXECUTORS_BLOCK_NAME))
      .filter(Optional::isPresent)
      .map(Optional::get)
      .filter(ExecutorInfo.class::isInstance)
      .map(ExecutorInfo.class::cast)
      .max(comparator);
}

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

private Ordering<Row> metricOrdering(final String column, final Comparator comparator)
{
 // As per SQL standard we need to have same ordering for metrics as dimensions i.e nulls first
 // If SQL compatibility is not enabled we use nullsLast ordering for null metrics for backwards compatibility.
 if (NullHandling.sqlCompatible()) {
  return Ordering.from(Comparator.comparing((Row row) -> row.getRaw(column), Comparator.nullsFirst(comparator)));
 } else {
  return Ordering.from(Comparator.comparing((Row row) -> row.getRaw(column), Comparator.nullsLast(comparator)));
 }
}

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

private UnaryOperator<ListResponse.Builder> addNotifications(DbSession dbSession, UserDto user) {
 return response -> {
  List<PropertyDto> properties = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setUserId(user.getId()).build(), dbSession);
  Map<Long, ComponentDto> componentsById = searchProjects(dbSession, properties);
  Map<String, OrganizationDto> organizationsByUuid = getOrganizations(dbSession, componentsById.values());
  Predicate<PropertyDto> isNotification = prop -> prop.getKey().startsWith("notification.");
  Predicate<PropertyDto> isComponentInDb = prop -> prop.getResourceId() == null || componentsById.containsKey(prop.getResourceId());
  Notification.Builder notification = Notification.newBuilder();
  properties.stream()
   .filter(isNotification)
   .filter(channelAndDispatcherAuthorized())
   .filter(isComponentInDb)
   .map(toWsNotification(notification, organizationsByUuid, componentsById))
   .sorted(comparing(Notification::getProject, nullsFirst(naturalOrder()))
    .thenComparing(comparing(Notification::getChannel))
    .thenComparing(comparing(Notification::getType)))
   .forEach(response::addNotifications);
  return response;
 };
}

代码示例来源:origin: org.apache.ant/ant

Arrays.sort(keyArray, Comparator.nullsFirst(
  Comparator.comparing(this::getRelativeFilePath)));

代码示例来源:origin: allure-framework/allure2

comparing(Label::getName, nullsFirst(naturalOrder()))
        .thenComparing(Label::getValue, nullsFirst(naturalOrder()))
);
set.addAll(convert(testSuite.getLabels(), this::convert));

代码示例来源:origin: stackoverflow.com

Comparator<Student> cmp = Comparator.comparing(Student::getName,
  Comparator.nullsFirst(String.CASE_INSENSITIVE_ORDER));

代码示例来源:origin: com.vaadin/vaadin-server

/**
 * Returns a {@code RangeValidator} comparing values of a {@code Comparable}
 * type using their <i>natural order</i>. Passing null to either
 * {@code minValue} or {@code maxValue} means there is no limit in that
 * direction. Both limits may be null; this can be useful if the limits are
 * resolved programmatically.
 * <p>
 * Null is considered to be less than any non-null value. This means null
 * never passes validation if a minimum value is specified.
 *
 * @param <C>
 *            the {@code Comparable} value type
 * @param errorMessage
 *            the error message to return if validation fails, not null
 * @param minValue
 *            the least value of the accepted range or null for no limit
 * @param maxValue
 *            the greatest value of the accepted range or null for no limit
 * @return the new validator
 */
public static <C extends Comparable<? super C>> RangeValidator<C> of(
    String errorMessage, C minValue, C maxValue) {
  return new RangeValidator<>(errorMessage,
      Comparator.nullsFirst(Comparator.naturalOrder()), minValue,
      maxValue);
}

代码示例来源:origin: org.elasticsearch/elasticsearch

/**
 * Returns one active replica shard for the given shard id or <code>null</code> if
 * no active replica is found.
 *
 * Since replicas could possibly be on nodes with a older version of ES than
 * the primary is, this will return replicas on the highest version of ES.
 *
 */
public ShardRouting activeReplicaWithHighestVersion(ShardId shardId) {
  // It's possible for replicaNodeVersion to be null, when deassociating dead nodes
  // that have been removed, the shards are failed, and part of the shard failing
  // calls this method with an out-of-date RoutingNodes, where the version might not
  // be accessible. Therefore, we need to protect against the version being null
  // (meaning the node will be going away).
  return assignedShards(shardId).stream()
      .filter(shr -> !shr.primary() && shr.active())
      .filter(shr -> node(shr.currentNodeId()) != null)
      .max(Comparator.comparing(shr -> node(shr.currentNodeId()).node(),
              Comparator.nullsFirst(Comparator.comparing(DiscoveryNode::getVersion))))
      .orElse(null);
}

相关文章