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

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

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

Comparator.nullsLast介绍

暂无

代码示例

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

/**
  * Returns a comparator of {@link Optional} values which treats {@link Optional#empty} as greater
  * 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>> emptiesLast(Comparator<? super T> valueComparator) {
  checkNotNull(valueComparator);
  return Comparator.comparing(o -> o.orElse(null), Comparator.nullsLast(valueComparator));
 }
}

代码示例来源: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: 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: OryxProject/oryx

/**
 * @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);
}

代码示例来源:origin: OryxProject/oryx

/**
 * @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);
}

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

/**
  * Returns a comparator of {@link Optional} values which treats {@link Optional#empty} as greater
  * 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>> emptiesLast(Comparator<? super T> valueComparator) {
  checkNotNull(valueComparator);
  return Comparator.comparing(o -> o.orElse(null), Comparator.nullsLast(valueComparator));
 }
}

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

/**
  * Returns a comparator of {@link Optional} values which treats {@link Optional#empty} as greater
  * 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>> emptiesLast(Comparator<? super T> valueComparator) {
  checkNotNull(valueComparator);
  return Comparator.comparing(o -> o.orElse(null), Comparator.nullsLast(valueComparator));
 }
}

代码示例来源: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: facebook/litho

/** Compares two {@link MethodParamModel}s based on name and annotations only. */
 public static Comparator<MethodParamModel> shallowParamComparator() {
  return Comparator.nullsLast(
    Comparator.comparing(MethodParamModel::getName)
      .thenComparing(
        (a, b) ->
          (a.getTypeName() == null && a.getTypeName() == b.getTypeName())
              || a.getTypeName().equals(b.getTypeName())
            ? 0
            : -1)
      .thenComparing((a, b) -> a.getAnnotations().equals(b.getAnnotations()) ? 0 : -1));
 }
}

代码示例来源:origin: kiegroup/optaplanner

ConstraintMatchTotal::getConstraintWeight, Comparator.nullsLast(Comparator.reverseOrder()));
constraintMatchTotals.stream()
    .sorted(constraintWeightComparator

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

@SuppressWarnings({ "unchecked", "rawtypes" })
private static int compareComparables(Object a, Object b) {
  return ((Comparator) Comparator
      .nullsLast(Comparator.naturalOrder())).compare(a, b);
}

代码示例来源:origin: LearnLib/automatalib

@Override
  <T> Comparator<T> toComparator(Comparator<? super T> baseComp) {
    return Comparator.nullsLast(baseComp);
  }
};

代码示例来源:origin: org.n52.janmayen/janmayen

/**
 * Creates a comparator wrapping {@code comparator} to sort by the optional's value, {@code null}s last.
 *
 * @param <U>        the optional's type
 * @param comparator the comparator
 *
 * @return the new comparator
 */
public static <U> Comparator<Optional<U>> nullsLast(Comparator<? super U> comparator) {
  return Comparator.comparing(Optionals::orElseNull, Comparator.nullsLast(comparator));
}

代码示例来源:origin: io.dropwizard/dropwizard-jersey

@Override
  public int compare(EndpointLogLine endpointA, EndpointLogLine endpointB) {
    return ComparisonChain.start()
      .compare(endpointA.basePath, endpointB.basePath)
      .compare(endpointA.httpMethod, endpointB.httpMethod, Comparator.nullsLast(Ordering.natural()))
      .result();
  }
}

代码示例来源:origin: protegeproject/protege

public PluginInfoTableModel() {
  bundles = Arrays.stream(ProtegeApplication.getContext().getBundles())
      .filter(ProtegeApplication::isPlugin)
      .sorted(comparing(
          b -> (String) b.getHeaders().get(Constants.BUNDLE_NAME),
          nullsLast(CASE_INSENSITIVE_ORDER)))
      .collect(toList());
}

代码示例来源:origin: io.sphere.sdk.jvm/sphere-models

public static Comparator<ProductProjection> comparingCategoryOrderHints(final String categoryId) {
    final Function<ProductProjection, String> orderHintExtractor =
        p -> Optional.ofNullable(p.getCategoryOrderHints())
        .map(categoryOrderHints -> categoryOrderHints.get(categoryId))
        .orElse(null);
    return comparing(orderHintExtractor, Comparator.nullsLast(Comparator.<String>naturalOrder()));
  }
}

代码示例来源:origin: com.powsybl/powsybl-triple-store-api

public List<String> pluck(String property) {
  return stream()
      .map(r -> r.get(property))
      .sorted(Comparator.nullsLast(String::compareTo))
      .collect(Collectors.toList());
}

代码示例来源:origin: org.apache.druid/druid-processing

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: OpenNMS/opennms

@Override
  public int compare(ResourceType r0, ResourceType r1) {
    // Sort by resource label, allowing the resource label to be null
    final Comparator<? super String> comparator = (a, b) -> a.compareTo(b);
    return Objects.compare(r0.getLabel(), r1.getLabel(), Comparator.nullsLast(comparator));
  }
});

代码示例来源:origin: commercetools/commercetools-jvm-sdk

private static Comparator<Product> comparatorOfStagedForCategory(final String categoryId) {
  final Function<Product, String> orderHintExtractor =
      p -> Optional.ofNullable(p.getMasterData().getStaged().getCategoryOrderHints())
          .map(categoryOrderHints -> categoryOrderHints.get(categoryId))
          .orElse(null);
  return comparing(orderHintExtractor, Comparator.nullsLast(Comparator.<String>naturalOrder()));
}

相关文章