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

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

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

Collectors.toCollection介绍

暂无

代码示例

代码示例来源:origin: ctripcorp/apollo

  1. public static List<OpenNamespaceDTO> batchTransformFromNamespaceBOs(List<NamespaceBO>
  2. namespaceBOs) {
  3. if (CollectionUtils.isEmpty(namespaceBOs)) {
  4. return Collections.emptyList();
  5. }
  6. List<OpenNamespaceDTO> openNamespaceDTOs =
  7. namespaceBOs.stream().map(OpenApiBeanUtils::transformFromNamespaceBO)
  8. .collect(Collectors.toCollection(LinkedList::new));
  9. return openNamespaceDTOs;
  10. }

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

  1. private static <T> Set<T> getProviderClasses(final Collection<ServiceHolder<T>> providers) {
  2. return providers.stream()
  3. .map(Providers::holder2service)
  4. .collect(Collectors.toCollection(LinkedHashSet::new));
  5. }

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

  1. /**
  2. * Return a set of all worker directories in root log directory.
  3. */
  4. public Set<File> getAllWorkerDirs() {
  5. File[] rootDirFiles = logRootDir.listFiles();
  6. if (rootDirFiles != null) {
  7. return Arrays.stream(rootDirFiles).flatMap(topoDir -> {
  8. File[] topoFiles = topoDir.listFiles();
  9. return topoFiles != null ? Arrays.stream(topoFiles) : Stream.empty();
  10. }).collect(toCollection(TreeSet::new));
  11. }
  12. return new TreeSet<>();
  13. }

代码示例来源:origin: org.assertj/assertj-core

  1. /**
  2. * Returns the declared fields of a given class excluding any synthetic or static fields.
  3. *
  4. * Synthetic fields are fields that are generated by the compiler for access purposes, or by instrumentation tools e.g. JaCoCo adds in a $jacocoData field
  5. * and therefore should be ignored when comparing fields.
  6. *
  7. * Static fields are used as constants, and are not associated with an object.
  8. *
  9. * @param clazz the class we want the declared fields.
  10. * @return the declared fields of given class excluding any synthetic fields.
  11. */
  12. private static Set<Field> getDeclaredFieldsIgnoringSyntheticAndStatic(Class<?> clazz) {
  13. return stream(clazz.getDeclaredFields()).filter(field -> !(field.isSynthetic()
  14. || Modifier.isStatic(field.getModifiers())))
  15. .collect(toCollection(LinkedHashSet::new));
  16. }

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

  1. public static int run() {
  2. List<Integer> integers = Arrays.asList(1, 2, 3);
  3. List<Integer> collected = integers.stream().collect(Collectors.toCollection(ArrayList::new));
  4. return collected.size();
  5. }

代码示例来源:origin: Graylog2/graylog2-server

  1. private static ReferenceList toReferenceList(Collection<Object> list) {
  2. // TODO: Support nested objects in list
  3. return list.stream()
  4. .map(ValueReference::of)
  5. .filter(Objects::nonNull)
  6. .collect(Collectors.toCollection(ReferenceList::new));
  7. }

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

  1. private static <T> Set<T> getProviderClasses(final Collection<ServiceHolder<T>> providers) {
  2. return providers.stream()
  3. .map(Providers::holder2service)
  4. .collect(Collectors.toCollection(LinkedHashSet::new));
  5. }

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

  1. public RolesConfig ofType(String pluginType) throws InvalidPluginTypeException {
  2. if (isBlank(pluginType)) {
  3. return this;
  4. }
  5. Class<? extends Role> roleClass = ROLE_FILTER_MAP.get(pluginType);
  6. if (roleClass == null) {
  7. throw new InvalidPluginTypeException("Bad role type `" + pluginType + "`. Valid values are " + StringUtils.join(ROLE_FILTER_MAP.keySet(), ", "));
  8. }
  9. return this.stream().filter(role -> role.getClass().isAssignableFrom(roleClass)).collect(Collectors.toCollection(RolesConfig::new));
  10. }
  11. }

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

  1. public static int run() {
  2. List<Integer> integers = Arrays.asList(1, 2, 3, 4);
  3. List<Integer> collected = integers.stream().collect(Collectors.toCollection(ArrayList::new));
  4. return collected.size();
  5. }

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

  1. @VisibleForTesting
  2. Set<File> selectDirsForCleanup(long nowMillis) {
  3. FileFilter fileFilter = mkFileFilterForLogCleanup(nowMillis);
  4. return Arrays.stream(logRootDir.listFiles())
  5. .flatMap(topoDir -> Arrays.stream(topoDir.listFiles(fileFilter)))
  6. .collect(toCollection(TreeSet::new));
  7. }

代码示例来源:origin: org.assertj/assertj-core

  1. private static Set<String> fieldsToName(Set<Field> fields) {
  2. return fields.stream().map(Field::getName).collect(toCollection(LinkedHashSet::new));
  3. }

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

  1. SegmentsCostCache(ArrayList<Bucket> sortedBuckets)
  2. {
  3. this.sortedBuckets = Preconditions.checkNotNull(sortedBuckets, "buckets should not be null");
  4. this.intervals = sortedBuckets.stream().map(Bucket::getInterval).collect(Collectors.toCollection(ArrayList::new));
  5. Preconditions.checkArgument(
  6. BUCKET_ORDERING.isOrdered(sortedBuckets),
  7. "buckets must be ordered by interval"
  8. );
  9. }

代码示例来源:origin: joel-costigliola/assertj-core

  1. /**
  2. * Returns the declared fields of a given class excluding any synthetic or static fields.
  3. *
  4. * Synthetic fields are fields that are generated by the compiler for access purposes, or by instrumentation tools e.g. JaCoCo adds in a $jacocoData field
  5. * and therefore should be ignored when comparing fields.
  6. *
  7. * Static fields are used as constants, and are not associated with an object.
  8. *
  9. * @param clazz the class we want the declared fields.
  10. * @return the declared fields of given class excluding any synthetic fields.
  11. */
  12. private static Set<Field> getDeclaredFieldsIgnoringSyntheticAndStatic(Class<?> clazz) {
  13. return stream(clazz.getDeclaredFields()).filter(field -> !(field.isSynthetic()
  14. || Modifier.isStatic(field.getModifiers())))
  15. .collect(toCollection(LinkedHashSet::new));
  16. }

代码示例来源:origin: shekhargulati/99-problems

  1. public static <T> T kthStream(final List<T> list, final int k) {
  2. return list.stream().limit(k + 1).collect(Collectors.toCollection(LinkedList::new)).getLast();
  3. }

代码示例来源:origin: org.assertj/assertj-core

  1. /**
  2. * Creates a <em>mutable</em> {@link ArrayList} containing the given elements.
  3. *
  4. * @param <T> the generic type of the {@code ArrayList} to create.
  5. * @param elements the elements to store in the {@code ArrayList}.
  6. * @return the created {@code ArrayList}, or {@code null} if the given {@code Iterator} is {@code null}.
  7. */
  8. public static <T> ArrayList<T> newArrayList(Iterator<? extends T> elements) {
  9. if (elements == null) {
  10. return null;
  11. }
  12. return Streams.stream(elements).collect(toCollection(ArrayList::new));
  13. }

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

  1. @VisibleForTesting
  2. JoinEnumerationResult createJoinAccordingToPartitioning(LinkedHashSet<PlanNode> sources, List<Symbol> outputSymbols, Set<Integer> partitioning)
  3. {
  4. List<PlanNode> sourceList = ImmutableList.copyOf(sources);
  5. LinkedHashSet<PlanNode> leftSources = partitioning.stream()
  6. .map(sourceList::get)
  7. .collect(toCollection(LinkedHashSet::new));
  8. LinkedHashSet<PlanNode> rightSources = sources.stream()
  9. .filter(source -> !leftSources.contains(source))
  10. .collect(toCollection(LinkedHashSet::new));
  11. return createJoin(leftSources, rightSources, outputSymbols);
  12. }

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

  1. private static Set<String> getRootQualifiers(ResourceTypes resourceTypes) {
  2. return resourceTypes.getRoots().stream()
  3. .map(ResourceType::getQualifier)
  4. .collect(Collectors.toCollection(TreeSet::new));
  5. }

代码示例来源:origin: org.assertj/assertj-core

  1. private void assertIterableContainsGivenValues(Iterable<?> actual, Object[] values, AssertionInfo info) {
  2. Set<Object> notFound = stream(values).filter(value -> !iterableContains(actual, value))
  3. .collect(toCollection(LinkedHashSet::new));
  4. if (notFound.isEmpty())
  5. return;
  6. throw failures.failure(info, shouldContain(actual, values, notFound, comparisonStrategy));
  7. }

代码示例来源:origin: org.springframework.boot/spring-boot

  1. /**
  2. * Return the classes from all the specified configurations in the order that they
  3. * would be registered.
  4. * @param configurations the source configuration
  5. * @return configuration classes in registration order
  6. */
  7. public static Class<?>[] getClasses(Collection<Configurations> configurations) {
  8. List<Configurations> ordered = new ArrayList<>(configurations);
  9. ordered.sort(COMPARATOR);
  10. List<Configurations> collated = collate(ordered);
  11. LinkedHashSet<Class<?>> classes = collated.stream()
  12. .flatMap(Configurations::streamClasses)
  13. .collect(Collectors.toCollection(LinkedHashSet::new));
  14. return ClassUtils.toClassArray(classes);
  15. }

代码示例来源:origin: org.assertj/assertj-core

  1. /**
  2. * Creates a <em>mutable</em> {@code HashSet} containing the given elements.
  3. *
  4. * @param <T> the generic type of the {@code HashSet} to create.
  5. * @param elements the elements to store in the {@code HashSet}.
  6. * @return the created {@code HashSet}, or {@code null} if the given array of elements is {@code null}.
  7. */
  8. public static <T> HashSet<T> newHashSet(Iterable<? extends T> elements) {
  9. if (elements == null) {
  10. return null;
  11. }
  12. return Streams.stream(elements).collect(toCollection(HashSet::new));
  13. }

相关文章