java.util.stream.Collectors类的使用及代码示例

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

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

Collectors介绍

暂无

代码示例

canonical example by Tabnine

private Double calculateAverageGrade(Map<String, List<Integer>> gradesList, String studentName)
  throws Exception {
 return Optional.ofNullable(gradesList.get(studentName))
   .map(list -> list.stream().collect(Collectors.averagingDouble(x -> x)))
   .orElseThrow(() -> new Exception("Student not found - " + studentName));
}

canonical example by Tabnine

public List<Integer> findDivisors(int number) {
 return Stream.iterate(1, k -> ++k)
   .limit(number)
   .filter(k -> number % k == 0)
   .collect(Collectors.toList());
}

canonical example by Tabnine

public void printFibonacciSequence(int length) {
 System.out.println(
   Stream.iterate(new long[] {0, 1}, pair -> new long[] {pair[1], pair[0] + pair[1]})
     .limit(length)
     .map(pair -> Long.toString(pair[1]))
     .collect(Collectors.joining(", ")));
}

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

@Override
public synchronized Optional<List<String>> getAllTables(String databaseName)
{
  requireNonNull(databaseName, "databaseName is null");
  Optional<Database> database = getDatabase(databaseName);
  if (!database.isPresent()) {
    return Optional.empty();
  }
  Path databaseMetadataDirectory = getDatabaseMetadataDirectory(databaseName);
  List<String> tables = getChildSchemaDirectories(databaseMetadataDirectory).stream()
      .map(Path::getName)
      .collect(toList());
  return Optional.of(ImmutableList.copyOf(tables));
}

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

private static Optional<Domain> getDomain(OptionalInt timestampOrdinalPosition, TupleDomain<LocalFileColumnHandle> predicate)
{
  Optional<Map<LocalFileColumnHandle, Domain>> domains = predicate.getDomains();
  Domain domain = null;
  if (domains.isPresent() && timestampOrdinalPosition.isPresent()) {
    Map<LocalFileColumnHandle, Domain> domainMap = domains.get();
    Set<Domain> timestampDomain = domainMap.entrySet().stream()
        .filter(entry -> entry.getKey().getOrdinalPosition() == timestampOrdinalPosition.getAsInt())
        .map(Map.Entry::getValue)
        .collect(toSet());
    if (!timestampDomain.isEmpty()) {
      domain = Iterables.getOnlyElement(timestampDomain);
    }
  }
  return Optional.ofNullable(domain);
}

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

private String formatHeaders(HttpHeaders headers, String delimiter) {
  return headers.entrySet().stream()
      .map(entry -> entry.getKey() + ": " + entry.getValue())
      .collect(Collectors.joining(delimiter));
}

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

private static List<PathPattern> parse(String[] paths, PathPatternParser parser) {
    return Arrays
        .stream(paths)
        .map(path -> {
          if (StringUtils.hasText(path) && !path.startsWith("/")) {
            path = "/" + path;
          }
          return parser.parse(path);
        })
        .collect(Collectors.toList());
  }
}

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

List<HivePartition> partitions = hiveLayoutHandle.getPartitions().get();
Optional<DiscretePredicates> discretePredicates = Optional.empty();
if (!partitionColumns.isEmpty()) {
  Iterable<TupleDomain<ColumnHandle>> partitionDomains = Iterables.transform(partitions, (hivePartition) -> TupleDomain.fromFixedValues(hivePartition.getKeys()));
  discretePredicates = Optional.of(new DiscretePredicates(partitionColumns, partitionDomains));
          hiveBucketHandle.getReadBucketCount(),
          hiveBucketHandle.getColumns().stream()
              .map(HiveColumnHandle::getHiveType)
              .collect(Collectors.toList()),
          OptionalInt.empty()),
      hiveBucketHandle.getColumns().stream()
          .map(ColumnHandle.class::cast)
          .collect(toList())));
    Optional.empty(),
    discretePredicates,
    ImmutableList.of());

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

.filter(TestColumn::isPartitionKey)
    .map(input -> new HivePartitionKey(input.getName(), (String) input.getWriteValue()))
    .collect(toList());
    split.getLength(),
    splitProperties,
    TupleDomain.all(),
    getColumnHandles(testColumns),
    partitionKeys,
    DateTimeZone.getDefault(),
    TYPE_MANAGER,
    ImmutableMap.of(),
    Optional.empty(),
    false);
RecordCursor cursor = ((RecordPageSource) pageSource.get()).getCursor();

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

@Override
public ActualProperties visitTableScan(TableScanNode node, List<ActualProperties> inputProperties)
{
  checkArgument(node.getLayout().isPresent(), "table layout has not yet been chosen");
  TableLayout layout = metadata.getLayout(session, node.getLayout().get());
  Map<ColumnHandle, Symbol> assignments = ImmutableBiMap.copyOf(node.getAssignments()).inverse();
  ActualProperties.Builder properties = ActualProperties.builder();
  // Globally constant assignments
  Map<ColumnHandle, NullableValue> globalConstants = new HashMap<>();
  extractFixedValues(node.getCurrentConstraint()).orElse(ImmutableMap.of())
      .entrySet().stream()
      .filter(entry -> !entry.getValue().isNull())
      .forEach(entry -> globalConstants.put(entry.getKey(), entry.getValue()));
  Map<Symbol, NullableValue> symbolConstants = globalConstants.entrySet().stream()
      .filter(entry -> assignments.containsKey(entry.getKey()))
      .collect(toMap(entry -> assignments.get(entry.getKey()), Map.Entry::getValue));
  properties.constants(symbolConstants);
  // Partitioning properties
  properties.global(deriveGlobalProperties(layout, assignments, globalConstants));
  // Append the global constants onto the local properties to maximize their translation potential
  List<LocalProperty<ColumnHandle>> constantAppendedLocalProperties = ImmutableList.<LocalProperty<ColumnHandle>>builder()
      .addAll(globalConstants.keySet().stream().map(ConstantProperty::new).iterator())
      .addAll(layout.getLocalProperties())
      .build();
  properties.local(LocalProperties.translate(constantAppendedLocalProperties, column -> Optional.ofNullable(assignments.get(column))));
  return properties.build();
}

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

/**
 * Get the physical layout for a inserting into an existing table.
 */
default Optional<ConnectorNewTableLayout> getInsertLayout(ConnectorSession session, ConnectorTableHandle tableHandle)
{
  List<ConnectorTableLayout> layouts = getTableLayouts(session, tableHandle, new Constraint<>(TupleDomain.all(), map -> true), Optional.empty())
      .stream()
      .map(ConnectorTableLayoutResult::getTableLayout)
      .filter(layout -> layout.getTablePartitioning().isPresent())
      .collect(toList());
  if (layouts.isEmpty()) {
    return Optional.empty();
  }
  if (layouts.size() > 1) {
    throw new PrestoException(NOT_SUPPORTED, "Tables with multiple layouts can not be written");
  }
  ConnectorTableLayout layout = layouts.get(0);
  ConnectorPartitioningHandle partitioningHandle = layout.getTablePartitioning().get().getPartitioningHandle();
  Map<ColumnHandle, String> columnNamesByHandle = getColumnHandles(session, tableHandle).entrySet().stream()
      .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
  List<String> partitionColumns = layout.getTablePartitioning().get().getPartitioningColumns().stream()
      .map(columnNamesByHandle::get)
      .collect(toList());
  return Optional.of(new ConnectorNewTableLayout(partitioningHandle, partitionColumns));
}

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

@Override
  public ConnectorSplitSource getSplits(ConnectorTransactionHandle transaction, ConnectorSession session, ConnectorTableLayoutHandle layout, SplitSchedulingStrategy splitSchedulingStrategy)
  {
    JmxTableLayoutHandle jmxLayout = (JmxTableLayoutHandle) layout;
    JmxTableHandle tableHandle = jmxLayout.getTable();
    TupleDomain<ColumnHandle> predicate = jmxLayout.getConstraint();

    //TODO is there a better way to get the node column?
    Optional<JmxColumnHandle> nodeColumnHandle = tableHandle.getColumnHandles().stream()
        .filter(jmxColumnHandle -> jmxColumnHandle.getColumnName().equals(NODE_COLUMN_NAME))
        .findFirst();
    checkState(nodeColumnHandle.isPresent(), "Failed to find %s column", NODE_COLUMN_NAME);

    List<ConnectorSplit> splits = nodeManager.getAllNodes().stream()
        .filter(node -> {
          NullableValue value = NullableValue.of(createUnboundedVarcharType(), utf8Slice(node.getNodeIdentifier()));
          return predicate.overlaps(fromFixedValues(ImmutableMap.of(nodeColumnHandle.get(), value)));
        })
        .map(node -> new JmxSplit(tableHandle, ImmutableList.of(node.getHostAndPort())))
        .collect(toList());

    return new FixedSplitSource(splits);
  }
}

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

@Override
public Set<Entry<String, List<String>>> entrySet() {
  return Collections.unmodifiableSet(this.headers.entrySet().stream()
      .map(AbstractMap.SimpleImmutableEntry::new)
      .collect(Collectors.toSet()));
}

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

Map<ColumnHandle, NullableValue> fixedValues = TupleDomain.extractFixedValues(tupleDomain).orElse(ImmutableMap.of())
    .entrySet().stream()
    .filter(entry -> !indexableColumns.contains(entry.getKey()))
    .filter(entry -> !entry.getValue().isNull()) // strip nulls since meaningless in index join lookups
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    .addAll(handleToNames(ImmutableList.copyOf(indexableColumns)))
    .addAll(handleToNames(ImmutableList.copyOf(fixedValues.keySet())))
    .build();
if (!indexedData.getIndexedTable(tpchTableHandle.getTableName(), tpchTableHandle.getScaleFactor(), lookupColumnNames).isPresent()) {
  return Optional.empty();
if (!tupleDomain.isNone()) {
  filteredTupleDomain = TupleDomain.withColumnDomains(Maps.filterKeys(tupleDomain.getDomains().get(), not(in(fixedValues.keySet()))));

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

public static RowType anonymous(List<Type> types)
{
  List<Field> fields = types.stream()
      .map(type -> new Field(Optional.empty(), type))
      .collect(Collectors.toList());
  return new RowType(makeSignature(fields), fields);
}

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

private static StorageDescriptor makeStorageDescriptor(String tableName, List<Column> columns, Storage storage)
{
  if (storage.isSkewed()) {
    throw new IllegalArgumentException("Writing to skewed table/partition is not supported");
  }
  SerDeInfo serdeInfo = new SerDeInfo();
  serdeInfo.setName(tableName);
  serdeInfo.setSerializationLib(storage.getStorageFormat().getSerDeNullable());
  serdeInfo.setParameters(storage.getSerdeParameters());
  StorageDescriptor sd = new StorageDescriptor();
  sd.setLocation(emptyToNull(storage.getLocation()));
  sd.setCols(columns.stream()
      .map(ThriftMetastoreUtil::toMetastoreApiFieldSchema)
      .collect(toList()));
  sd.setSerdeInfo(serdeInfo);
  sd.setInputFormat(storage.getStorageFormat().getInputFormatNullable());
  sd.setOutputFormat(storage.getStorageFormat().getOutputFormatNullable());
  sd.setParameters(ImmutableMap.of());
  Optional<HiveBucketProperty> bucketProperty = storage.getBucketProperty();
  if (bucketProperty.isPresent()) {
    sd.setNumBuckets(bucketProperty.get().getBucketCount());
    sd.setBucketCols(bucketProperty.get().getBucketedBy());
    if (!bucketProperty.get().getSortedBy().isEmpty()) {
      sd.setSortCols(bucketProperty.get().getSortedBy().stream()
          .map(column -> new Order(column.getColumnName(), column.getOrder().getHiveOrder()))
          .collect(toList()));
    }
  }
  return sd;
}

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

private Map<TpchColumn<?>, List<Object>> getColumnValuesRestrictions(TpchTable<?> tpchTable, Constraint<ColumnHandle> constraint)
{
  TupleDomain<ColumnHandle> constraintSummary = constraint.getSummary();
  if (constraintSummary.isAll()) {
    return emptyMap();
  }
  else if (constraintSummary.isNone()) {
    Set<TpchColumn<?>> columns = ImmutableSet.copyOf(tpchTable.getColumns());
    return asMap(columns, key -> emptyList());
  }
  else {
    Map<ColumnHandle, Domain> domains = constraintSummary.getDomains().get();
    Optional<Domain> orderStatusDomain = Optional.ofNullable(domains.get(toColumnHandle(ORDER_STATUS)));
    Optional<Map<TpchColumn<?>, List<Object>>> allowedColumnValues = orderStatusDomain.map(domain -> {
      List<Object> allowedValues = ORDER_STATUS_VALUES.stream()
          .filter(domain::includesNullableValue)
          .collect(toList());
      return avoidTrivialOrderStatusRestriction(allowedValues);
    });
    return allowedColumnValues.orElse(emptyMap());
  }
}

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

protected static Set<String> getSubProperties(Map<String, String> properties, String prefix) {
  return properties.keySet().stream().filter(k -> k.contains(prefix)).map(k -> {
    k = k.substring(prefix.length());
    return k.substring(0, k.indexOf("."));
  }).collect(Collectors.toSet());
}

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

/**
 * Extract all column constraints that require exactly one value or only null in their respective Domains.
 * Returns an empty Optional if the Domain is none.
 */
public static <T> Optional<Map<T, NullableValue>> extractFixedValues(TupleDomain<T> tupleDomain)
{
  if (!tupleDomain.getDomains().isPresent()) {
    return Optional.empty();
  }
  return Optional.of(tupleDomain.getDomains().get()
      .entrySet().stream()
      .filter(entry -> entry.getValue().isNullableSingleValue())
      .collect(toMap(Map.Entry::getKey, entry -> new NullableValue(entry.getValue().getType(), entry.getValue().getNullableSingleValue()))));
}

相关文章