本文整理了Java中java.util.stream.Stream.map()
方法的一些代码示例,展示了Stream.map()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Stream.map()
方法的具体详情如下:
包路径:java.util.stream.Stream
类名称:Stream
方法名:map
[英]Returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.
This is an intermediate operation.
[中]返回一个DoubleStream,其中包含将给定函数应用于该流元素的结果。
这是一个intermediate operation。
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: 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
private String formatMappings(Class<?> userType, Map<Method, T> methods) {
String formattedType = Arrays.stream(ClassUtils.getPackageName(userType).split("\\."))
.map(p -> p.substring(0, 1))
.collect(Collectors.joining(".", "", ".")) + userType.getSimpleName();
Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes())
.map(Class::getSimpleName)
.collect(Collectors.joining(",", "(", ")"));
return methods.entrySet().stream()
.map(e -> {
Method method = e.getKey();
return e.getValue() + ": " + method.getName() + methodFormatter.apply(method);
})
.collect(Collectors.joining("\n\t", "\n\t" + formattedType + ":" + "\n\t", ""));
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Provide the TaskScheduler to use for SockJS endpoints for which a task
* scheduler has not been explicitly registered. This method must be called
* prior to {@link #getHandlerMapping()}.
*/
protected void setTaskScheduler(TaskScheduler scheduler) {
this.registrations.stream()
.map(ServletWebSocketHandlerRegistration::getSockJsServiceRegistration)
.filter(Objects::nonNull)
.filter(r -> r.getTaskScheduler() == null)
.forEach(registration -> registration.setTaskScheduler(scheduler));
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Helps to format HTTP header values, as HTTP header values themselves can
* contain comma-separated values, can become confusing with regular
* {@link Map} formatting that also uses commas between entries.
* @param headers the headers to format
* @return the headers to a String
* @since 5.1.4
*/
public static String formatHeaders(MultiValueMap<String, String> headers) {
return headers.entrySet().stream()
.map(entry -> {
List<String> values = entry.getValue();
return entry.getKey() + ":" + (values.size() == 1 ?
"\"" + values.get(0) + "\"" :
values.stream().map(s -> "\"" + s + "\"").collect(Collectors.joining(", ")));
})
.collect(Collectors.joining(", ", "[", "]"));
}
代码示例来源:origin: spring-projects/spring-framework
@Nullable
private String getContentCodingKey(HttpServletRequest request) {
String header = request.getHeader(HttpHeaders.ACCEPT_ENCODING);
if (!StringUtils.hasText(header)) {
return null;
}
return Arrays.stream(StringUtils.tokenizeToStringArray(header, ","))
.map(token -> {
int index = token.indexOf(';');
return (index >= 0 ? token.substring(0, index) : token).trim().toLowerCase();
})
.filter(this.contentCodings::contains)
.sorted()
.collect(Collectors.joining(","));
}
代码示例来源:origin: apache/incubator-dubbo
public static int getConsumerAddressNum(String serviceUniqueName) {
Set<ConsumerInvokerWrapper> providerInvokerWrapperSet = ProviderConsumerRegTable.getConsumerInvoker(serviceUniqueName);
return providerInvokerWrapperSet.stream()
.map(w -> w.getRegistryDirectory().getUrlInvokerMap())
.filter(Objects::nonNull)
.mapToInt(Map::size).sum();
}
}
代码示例来源:origin: spring-projects/spring-framework
@Override
@Nullable
public CorsConfiguration getCorsConfiguration(ServerWebExchange exchange) {
PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication();
return this.corsConfigurations.entrySet().stream()
.filter(entry -> entry.getKey().matches(lookupPath))
.map(Map.Entry::getValue)
.findFirst()
.orElse(null);
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public Stream<T> stream() {
return Arrays.stream(getBeanNamesForTypedStream(requiredType))
.map(name -> (T) getBean(name))
.filter(bean -> !(bean instanceof NullBean));
}
@Override
代码示例来源:origin: spring-projects/spring-framework
@Override
public boolean containsValue(Object value) {
return (value instanceof String &&
this.headers.getHeaderNames().stream()
.map(this.headers::get)
.anyMatch(values -> values.contains(value)));
}
代码示例来源: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: spring-projects/spring-framework
private String formatMappings(Class<?> userType, Map<Method, T> methods) {
String formattedType = Arrays.stream(ClassUtils.getPackageName(userType).split("\\."))
.map(p -> p.substring(0, 1))
.collect(Collectors.joining(".", "", ".")) + userType.getSimpleName();
Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes())
.map(Class::getSimpleName)
.collect(Collectors.joining(",", "(", ")"));
return methods.entrySet().stream()
.map(e -> {
Method method = e.getKey();
return e.getValue() + ": " + method.getName() + methodFormatter.apply(method);
})
.collect(Collectors.joining("\n\t", "\n\t" + formattedType + ":" + "\n\t", ""));
}
代码示例来源:origin: spring-projects/spring-framework
private void assertDecoderInstance(Decoder<?> decoder) {
assertSame(decoder, this.configurer.getReaders().stream()
.filter(writer -> writer instanceof DecoderHttpMessageReader)
.map(writer -> ((DecoderHttpMessageReader<?>) writer).getDecoder())
.filter(e -> decoder.getClass().equals(e.getClass()))
.findFirst()
.filter(e -> e == decoder).orElse(null));
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Return declared HTTP methods.
*/
public Set<HttpMethod> getAllowedMethods() {
return this.partialMatches.stream().
flatMap(m -> m.getInfo().getMethodsCondition().getMethods().stream()).
map(requestMethod -> HttpMethod.resolve(requestMethod.name())).
collect(Collectors.toSet());
}
代码示例来源:origin: spring-projects/spring-framework
@Nullable
private String getContentCodingKey(ServerWebExchange exchange) {
String header = exchange.getRequest().getHeaders().getFirst("Accept-Encoding");
if (!StringUtils.hasText(header)) {
return null;
}
return Arrays.stream(StringUtils.tokenizeToStringArray(header, ","))
.map(token -> {
int index = token.indexOf(';');
return (index >= 0 ? token.substring(0, index) : token).trim().toLowerCase();
})
.filter(this.contentCodings::contains)
.sorted()
.collect(Collectors.joining(","));
}
代码示例来源:origin: apache/incubator-dubbo
public static int getConsumerAddressNum(String serviceUniqueName) {
Set<ConsumerInvokerWrapper> providerInvokerWrapperSet = ProviderConsumerRegTable.getConsumerInvoker(serviceUniqueName);
return providerInvokerWrapperSet.stream()
.map(w -> w.getRegistryDirectory().getUrlInvokerMap())
.filter(Objects::nonNull)
.mapToInt(Map::size).sum();
}
}
代码示例来源:origin: spring-projects/spring-framework
private int countAnnotatedMethods(Class<? extends Annotation> annotationType) {
return (int) Arrays.stream(this.testClasses)
.map(ReflectionUtils::getUniqueDeclaredMethods)
.flatMap(Arrays::stream)
.filter(method -> hasAnnotation(method, annotationType))
.count();
}
代码示例来源:origin: spring-projects/spring-framework
@Override
protected void applyCookies() {
getCookies().values().stream().flatMap(Collection::stream)
.map(cookie -> new DefaultCookie(cookie.getName(), cookie.getValue()))
.forEach(this.request::addCookie);
}
代码示例来源: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.");
}
}
内容来源于网络,如有侵权,请联系作者删除!