本文整理了Java中java.util.stream.Stream.collect()
方法的一些代码示例,展示了Stream.collect()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Stream.collect()
方法的具体详情如下:
包路径:java.util.stream.Stream
类名称:Stream
方法名:collect
[英]Performs a mutable reduction operation on the elements of this stream. A mutable reduction is one in which the reduced value is a mutable result container, such as an ArrayList, and elements are incorporated by updating the state of the result rather than by replacing the result. This produces a result equivalent to:
R result = supplier.get();
Like #reduce(Object,BinaryOperator), collect operations can be parallelized without requiring additional synchronization.
This is a terminal operation.
[中]在此流的元素上执行{$0$}操作。可变缩减是指缩减值是可变结果容器(如ArrayList),通过更新结果状态而不是替换结果来合并元素。这将产生相当于以下内容的结果:
R result = supplier.get();
与#reduce(Object,BinaryOperator)类似,collect操作可以并行化,而不需要额外的同步。
这是一个terminal operation。
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: spring-projects/spring-framework
/**
* Re-create the given mime types as media types.
* @since 5.0
*/
public static List<MediaType> asMediaTypes(List<MimeType> mimeTypes) {
return mimeTypes.stream().map(MediaType::asMediaType).collect(Collectors.toList());
}
代码示例来源: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: spring-projects/spring-framework
@Override
public Set<String> keySet() {
return this.headers.getHeaderNames().stream()
.map(HttpString::toString)
.collect(Collectors.toSet());
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Return declared "producible" types but only among those that also
* match the "methods" and "consumes" conditions.
*/
public Set<MediaType> getProducibleMediaTypes() {
return this.partialMatches.stream().filter(PartialMatch::hasConsumesMatch).
flatMap(m -> m.getInfo().getProducesCondition().getProducibleMediaTypes().stream()).
collect(Collectors.toCollection(LinkedHashSet::new));
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Get all methods in the supplied {@link Class class} and its superclasses
* which are annotated with the supplied {@code annotationType} but
* which are not <em>shadowed</em> by methods overridden in subclasses.
* <p>Default methods on interfaces are also detected.
* @param clazz the class for which to retrieve the annotated methods
* @param annotationType the annotation type for which to search
* @return all annotated methods in the supplied class and its superclasses
* as well as annotated interface default methods
*/
private List<Method> getAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationType) {
return Arrays.stream(ReflectionUtils.getUniqueDeclaredMethods(clazz))
.filter(method -> AnnotatedElementUtils.hasAnnotation(method, annotationType))
.collect(Collectors.toList());
}
代码示例来源:origin: spring-projects/spring-framework
private Mono<Map<String, Object>> initAttributes(ServerWebExchange exchange) {
if (this.sessionAttributePredicate == null) {
return EMPTY_ATTRIBUTES;
}
return exchange.getSession().map(session ->
session.getAttributes().entrySet().stream()
.filter(entry -> this.sessionAttributePredicate.test(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
}
代码示例来源:origin: apache/incubator-dubbo
private <T> List<Invoker<T>> filterInvoker(List<Invoker<T>> invokers, Predicate<Invoker<T>> predicate) {
return invokers.stream()
.filter(predicate)
.collect(Collectors.toList());
}
代码示例来源: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: spring-projects/spring-framework
/**
* Return declared "consumable" types but only among those that also
* match the "methods" condition.
*/
public Set<MediaType> getConsumableMediaTypes() {
return this.partialMatches.stream().filter(PartialMatch::hasMethodsMatch).
flatMap(m -> m.getInfo().getConsumesCondition().getConsumableMediaTypes().stream()).
collect(Collectors.toCollection(LinkedHashSet::new));
}
代码示例来源:origin: apache/kafka
private Set<String> topics() {
return updateResponse.topicMetadata().stream()
.map(MetadataResponse.TopicMetadata::topic)
.collect(Collectors.toSet());
}
}
代码示例来源:origin: apache/incubator-dubbo
private <T> List<Invoker<T>> filterInvoker(List<Invoker<T>> invokers, Predicate<Invoker<T>> predicate) {
return invokers.stream()
.filter(predicate)
.collect(Collectors.toList());
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Return all registered interceptors.
*/
protected List<Object> getInterceptors() {
return this.registrations.stream()
.sorted(INTERCEPTOR_ORDER_COMPARATOR)
.map(InterceptorRegistration::getInterceptor)
.collect(Collectors.toList());
}
代码示例来源:origin: apache/incubator-dubbo
public static List<URL> classifyUrls(List<URL> urls, Predicate<URL> predicate) {
return urls.stream().filter(predicate).collect(Collectors.toList());
}
代码示例来源:origin: apache/incubator-dubbo
public static List<URL> classifyUrls(List<URL> urls, Predicate<URL> predicate) {
return urls.stream().filter(predicate).collect(Collectors.toList());
}
代码示例来源:origin: spring-projects/spring-framework
@Nullable
private <T> T createSingleBean(Function<WebFluxConfigurer, T> factory, Class<T> beanType) {
List<T> result = this.delegates.stream().map(factory).filter(Objects::nonNull).collect(Collectors.toList());
if (result.isEmpty()) {
return null;
}
else if (result.size() == 1) {
return result.get(0);
}
else {
throw new IllegalStateException("More than one WebFluxConfigurer implements " +
beanType.getSimpleName() + " factory method.");
}
}
代码示例来源:origin: spring-projects/spring-framework
private List<SyncHandlerMethodArgumentResolver> initBinderResolvers(
ArgumentResolverConfigurer customResolvers, ReactiveAdapterRegistry reactiveRegistry,
ConfigurableApplicationContext context) {
return initResolvers(customResolvers, reactiveRegistry, context, false, Collections.emptyList()).stream()
.filter(resolver -> resolver instanceof SyncHandlerMethodArgumentResolver)
.map(resolver -> (SyncHandlerMethodArgumentResolver) resolver)
.collect(Collectors.toList());
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public Collection<List<String>> values() {
return this.headers.getHeaderNames().stream()
.map(this.headers::get)
.collect(Collectors.toList());
}
内容来源于网络,如有侵权,请联系作者删除!