java.util.stream.Stream.distinct()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(146)

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

Stream.distinct介绍

[英]Returns a stream consisting of the distinct elements (according to Object#equals(Object)) of this stream.

For ordered streams, the selection of distinct elements is stable (for duplicated elements, the element appearing first in the encounter order is preserved.) For unordered streams, no stability guarantees are made.

This is a stateful intermediate operation.
[中]返回由该流的不同元素(根据对象#equals(Object))组成的流。
对于有序流,不同元素的选择是稳定的(对于重复的元素,将保留相遇顺序中出现的第一个元素)对于无序流,不保证稳定性。
这是一个stateful intermediate operation

代码示例

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

public List<Symbol> getOriginalNonDistinctAggregateArgs()
{
  return aggregations.values().stream()
      .filter(aggregation -> !aggregation.getMask().isPresent())
      .map(Aggregation::getCall)
      .flatMap(function -> function.getArguments().stream())
      .distinct()
      .map(Symbol::from)
      .collect(Collectors.toList());
}

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

private static Stream<Set<Expression>> extractArgumentSets(AggregationNode aggregation)
{
  return aggregation.getAggregations()
      .values().stream()
      .map(Aggregation::getCall)
      .filter(FunctionCall::isDistinct)
      .map(FunctionCall::getArguments)
      .<Set<Expression>>map(HashSet::new)
      .distinct();
}

代码示例来源:origin: Vedenin/useful-java-links

private static void testDistinct() {
  System.out.println();
  System.out.println("Test distinct start");
  Collection<String> ordered = Arrays.asList("a1", "a2", "a2", "a3", "a1", "a2", "a2");
  Collection<String> nonOrdered = new HashSet<>(ordered);
  // Get collection without duplicate
  List<String> distinct = nonOrdered.stream().distinct().collect(Collectors.toList());
  System.out.println("distinct = " + distinct); // print  distinct = [a1, a2, a3] - порядок не гарантируется
  List<String> distinctOrdered = ordered.stream().distinct().collect(Collectors.toList());
  System.out.println("distinctOrdered = " + distinctOrdered); // print  distinct = [a1, a2, a3] - порядок гарантируется
}

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

private <T> Stream<? extends T> streamResources(
    Function<? super Resource, ? extends T> mapper) {
    return getResourceCollections().stream()
      .flatMap(ResourceCollection::stream).map(mapper).distinct();
  }
}

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

private void distinct() {
  Set<String> ratings = films.stream()
      .map(Film.RATING)
      .distinct()
      .collect(Collectors.toSet());
  System.out.println(ratings);
}

代码示例来源:origin: fesh0r/fernflower

public String getUniqueExceptionsString() {
  return exceptionTypes != null ? exceptionTypes.stream().distinct().collect(Collectors.joining(":")) : null;
 }
}

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

@Override
public int[] fetchSegments(String processorName) {
  return tokens.keySet().stream()
         .filter(ps -> ps.processorName.equals(processorName))
         .map(ProcessAndSegment::getSegment)
         .distinct().mapToInt(Number::intValue).toArray();
}

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

private Optional<Predicate<Throwable>> buildRecordExceptionsPredicate() {
  return Arrays.stream(recordExceptions)
      .distinct()
      .map(Builder::makePredicate)
      .reduce(Predicate::or);
}

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

@Override
public GameObject[] result(Client client)
{
  return getGameObjects(client).stream()
    .filter(Objects::nonNull)
    .filter(predicate)
    .distinct()
    .toArray(GameObject[]::new);
}

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

private static Map<Expression, Symbol> symbolsForExpressions(PlanBuilder builder, Iterable<? extends Expression> expressions)
{
  return stream(expressions)
      .distinct()
      .collect(toImmutableMap(expression -> expression, builder::translate));
}

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

public List<Symbol> getOriginalDistinctAggregateArgs()
{
  return aggregations.values().stream()
      .filter(aggregation -> aggregation.getMask().isPresent())
      .map(Aggregation::getCall)
      .flatMap(function -> function.getArguments().stream())
      .distinct()
      .map(Symbol::from)
      .collect(Collectors.toList());
}

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

@Test
public void testNonDeterministic()
{
  MaterializedResult materializedResult = computeActual("SELECT rand() FROM orders LIMIT 10");
  long distinctCount = materializedResult.getMaterializedRows().stream()
      .map(row -> row.getField(0))
      .distinct()
      .count();
  assertTrue(distinctCount >= 8, "rand() must produce different rows");
  materializedResult = computeActual("SELECT apply(1, x -> x + rand()) FROM orders LIMIT 10");
  distinctCount = materializedResult.getMaterializedRows().stream()
      .map(row -> row.getField(0))
      .distinct()
      .count();
  assertTrue(distinctCount >= 8, "rand() must produce different rows");
}

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

@Override
public List<String> listSchemaNames(ConnectorSession session)
{
  return tables.listSystemTables(session).stream()
      .map(table -> table.getTableMetadata().getTable().getSchemaName())
      .distinct()
      .collect(toImmutableList());
}

代码示例来源:origin: jtablesaw/tablesaw

default StringColumn tokenizeAndRemoveDuplicates(String separator) {
    StringColumn newColumn = StringColumn.create(name() + "[without duplicates]", this.size());

    for (int r = 0; r < size(); r++) {
      String value = getString(r);

      Splitter splitter = Splitter.on(separator);
      splitter = splitter.trimResults();
      splitter = splitter.omitEmptyStrings();
      List<String> tokens = new ArrayList<>(splitter.splitToList(value));

      String result = tokens.stream().distinct().collect(Collectors.joining(separator));
      newColumn.set(r, result);
    }
    return newColumn;
  }
}

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

private static boolean hasMultipleDistincts(AggregationNode aggregation)
{
  return aggregation.getAggregations()
      .values().stream()
      .filter(e -> e.getCall().isDistinct())
      .map(Aggregation::getCall)
      .map(FunctionCall::getArguments)
      .map(HashSet::new)
      .distinct()
      .count() > 1;
}

代码示例来源:origin: spring-io/initializr

/**
 * Return all dependencies as a flat collection.
 * @return all dependencies
 */
public Collection<Dependency> getAll() {
  return Collections.unmodifiableCollection(this.indexedDependencies.values()
      .stream().distinct().collect(Collectors.toList()));
}

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

private Optional<Predicate<Throwable>> buildRetryExceptionsPredicate() {
  return Arrays.stream(retryExceptions)
      .distinct()
      .map(Builder::makePredicate)
      .reduce(Predicate::or);
}

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

@Override
public GroundObject[] result(Client client)
{
  return getGroundObjects(client).stream()
    .filter(Objects::nonNull)
    .filter(predicate)
    .distinct()
    .toArray(GroundObject[]::new);
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Set the allowed HTTP methods.
 *
 * @param methods The methods to specify in the Allowed HTTP header
 * @return This HTTP headers
 */
default MutableHttpHeaders allow(Collection<HttpMethod> methods) {
  String value = methods.stream().distinct().collect(Collectors.joining(","));
  return add(ALLOW, value);
}

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

@Override
public List<String> getDataSourceNames()
{
 return rels.stream()
       .flatMap(rel -> ((DruidRel<?>) rel).getDataSourceNames().stream())
       .distinct()
       .collect(Collectors.toList());
}

相关文章