本文整理了Java中java.util.stream.Stream.findFirst()
方法的一些代码示例,展示了Stream.findFirst()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Stream.findFirst()
方法的具体详情如下:
包路径:java.util.stream.Stream
类名称:Stream
方法名:findFirst
[英]Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned.
This is a short-circuiting terminal operation.
[中]返回描述此流的第一个元素的可选值,如果流为空,则返回空可选值。如果流没有遭遇顺序,那么可以返回任何元素。
这是一个short-circuiting terminal operation。
代码示例来源:origin: spring-projects/spring-framework
private static Log initLogger(List<Log> loggers, Predicate<Log> predicate) {
return loggers.stream().filter(predicate).findFirst().orElse(NO_OP_LOG);
}
代码示例来源:origin: apache/incubator-dubbo
/**
* Return method model for the given method on consumer side
*
* @param method method object
* @return method model
*/
public ConsumerMethodModel getMethodModel(String method) {
Optional<Map.Entry<Method, ConsumerMethodModel>> consumerMethodModelEntry = methodModels.entrySet().stream().filter(entry -> entry.getKey().getName().equals(method)).findFirst();
return consumerMethodModelEntry.map(Map.Entry::getValue).orElse(null);
}
代码示例来源:origin: jenkinsci/jenkins
private @Nonnull Optional<SingleTokenStats> findById(@Nonnull String tokenUuid) {
return tokenStats.stream()
.filter(s -> s.tokenUuid.equals(tokenUuid))
.findFirst();
}
代码示例来源:origin: micronaut-projects/micronaut-core
@SuppressWarnings("unchecked")
@Override
public Optional<? extends ExecutableMethod<?, ?>> findFirst() {
return Stream.of(functions, suppliers, consumers, biFunctions)
.map(all -> {
Collection<ExecutableMethod<?, ?>> values = all.values();
return values.stream().findFirst();
}).filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
}
代码示例来源:origin: org.apache.ant/ant
/**
* Returns the topmost interface that extends Remote for a given
* class - if one exists.
* @param testClass the class to be tested
* @return the topmost interface that extends Remote, or null if there
* is none.
*/
public Class<?> getRemoteInterface(Class<?> testClass) {
return Stream.of(testClass.getInterfaces())
.filter(Remote.class::isAssignableFrom).findFirst().orElse(null);
}
代码示例来源:origin: spring-projects/spring-framework
private static <T> HttpMessageWriter<T> findWriter(
BodyInserter.Context context, ResolvableType elementType, @Nullable MediaType mediaType) {
return context.messageWriters().stream()
.filter(messageWriter -> messageWriter.canWrite(elementType, mediaType))
.findFirst()
.map(BodyInserters::<T>cast)
.orElseThrow(() -> new IllegalStateException(
"No HttpMessageWriter for \"" + mediaType + "\" and \"" + elementType + "\""));
}
代码示例来源:origin: SonarSource/sonarqube
private void migrateGlobalWebhooks(Context context, Collection<PropertyRow> rows) throws SQLException {
Multimap<String, PropertyRow> rowsByPropertyKey = rows.stream()
.collect(MoreCollectors.index(PropertyRow::getPropertyKey));
Optional<PropertyRow> rootProperty = rowsByPropertyKey.get("sonar.webhooks.global").stream().findFirst();
if (rootProperty.isPresent()) {
PropertyRow row = rootProperty.get();
// can't lambda due to checked exception.
for (Webhook webhook : extractGlobalWebhooksFrom(context, rowsByPropertyKey, row.value().split(","))) {
insert(context, webhook);
}
}
}
代码示例来源:origin: prestodb/presto
private Optional<ColumnHandle> getColumnHandle(TableHandle tableHandle, Session session, Metadata metadata)
{
return metadata.getColumnHandles(session, tableHandle).entrySet()
.stream()
.filter(entry -> columnName.equals(entry.getKey()))
.map(Map.Entry::getValue)
.findFirst();
}
代码示例来源:origin: requery/requery
static Optional<ImmutableAnnotationKind> of(Element element) {
return Stream.of(ImmutableAnnotationKind.values())
.filter(type -> type.isPresent(element)).findFirst();
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Return the first {@link Locale} of the content languages,
* as specified by the {@literal Content-Language} header.
* <p>Returns {@code null} when the content language is unknown.
* <p>Use {@code getValuesAsList(CONTENT_LANGUAGE)} if you need
* to get multiple content languages.</p>
* @since 5.0
*/
@Nullable
public Locale getContentLanguage() {
return getValuesAsList(CONTENT_LANGUAGE)
.stream()
.findFirst()
.map(Locale::forLanguageTag)
.orElse(null);
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Determine a media type for the given file name, if possible.
* @param filename the file name plus extension
* @return the corresponding media type, or {@code null} if none found
*/
public static Optional<MediaType> getMediaType(@Nullable String filename) {
return getMediaTypes(filename).stream().findFirst();
}
代码示例来源:origin: spring-projects/spring-framework
private static Method getMethodForReturnType(Class<?> returnType) {
return Arrays.stream(TestBean.class.getMethods())
.filter(method -> method.getReturnType().equals(returnType))
.findFirst()
.orElseThrow(() ->
new IllegalArgumentException("Unique return type not found: " + returnType));
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
// Remove underlying context path first (e.g. Servlet container)
String path = request.getPath().pathWithinApplication().value();
return this.handlerMap.entrySet().stream()
.filter(entry -> path.startsWith(entry.getKey()))
.findFirst()
.map(entry -> {
String contextPath = request.getPath().contextPath().value() + entry.getKey();
ServerHttpRequest newRequest = request.mutate().contextPath(contextPath).build();
return entry.getValue().handle(newRequest, response);
})
.orElseGet(() -> {
response.setStatusCode(HttpStatus.NOT_FOUND);
return response.setComplete();
});
}
代码示例来源:origin: prestodb/presto
private void verifyNoOrderByReferencesToOutputColumns(Node node, SemanticErrorCode errorCode, String errorString)
{
getReferencesToScope(node, analysis, orderByScope.get())
.findFirst()
.ifPresent(expression -> {
throw new SemanticException(errorCode, expression, errorString);
});
}
}
代码示例来源:origin: SonarSource/sonarqube
private String guessResponseType(String path, String action) {
return guessResponseOuterClassName(path).flatMap(
potentialClassName -> guessResponseInnerClassName(action).flatMap(potentialInnerClassName -> {
try {
String guess = "org.sonarqube.ws." + potentialClassName + "$" + potentialInnerClassName;
Helper.class.forName(guess);
return Stream.of(guess.replaceFirst("\\$", "."));
} catch (ClassNotFoundException e) {
}
return Stream.empty();
})).findFirst().orElseGet(() -> {
return "String";
});
}
代码示例来源:origin: spring-projects/spring-framework
@Nullable
private static MediaType initDefaultMediaType(List<MediaType> mediaTypes) {
return mediaTypes.stream().filter(MediaType::isConcrete).findFirst().orElse(null);
}
代码示例来源:origin: apache/incubator-druid
private static String findVersion(Map<Interval, String> versions, Interval interval)
{
return versions.entrySet().stream()
.filter(entry -> entry.getKey().contains(interval))
.map(Entry::getValue)
.findFirst()
.orElseThrow(() -> new ISE("Cannot find a version for interval[%s]", interval));
}
代码示例来源:origin: SonarSource/sonarqube
private static File locateHomeDir(Configuration configuration) {
return Stream.of(
configuration.get("sonar.userHome").orElse(null),
System.getenv("SONAR_USER_HOME"),
System.getProperty("user.home") + File.separator + ".sonar")
.filter(Objects::nonNull)
.findFirst()
.map(File::new)
.get();
}
}
代码示例来源:origin: spring-projects/spring-framework
private static <T> HttpMessageReader<T> findReader(
ResolvableType elementType, MediaType mediaType, BodyExtractor.Context context) {
return context.messageReaders().stream()
.filter(messageReader -> messageReader.canRead(elementType, mediaType))
.findFirst()
.map(BodyExtractors::<T>cast)
.orElseThrow(() -> new IllegalStateException(
"No HttpMessageReader for \"" + mediaType + "\" and \"" + elementType + "\""));
}
代码示例来源:origin: apache/incubator-dubbo
/**
* Return method model for the given method on consumer side
*
* @param method method object
* @return method model
*/
public ConsumerMethodModel getMethodModel(String method) {
Optional<Map.Entry<Method, ConsumerMethodModel>> consumerMethodModelEntry = methodModels.entrySet().stream().filter(entry -> entry.getKey().getName().equals(method)).findFirst();
return consumerMethodModelEntry.map(Map.Entry::getValue).orElse(null);
}
内容来源于网络,如有侵权,请联系作者删除!