首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Java 8实现嵌套Nullable列表上的迭代

如何使用Java 8实现嵌套Nullable列表上的迭代
EN

Stack Overflow用户
提问于 2022-04-29 12:40:39
回答 3查看 79关注 0票数 0

如何使用Java 8实现多个循环的迭代?

我需要检查一个嵌套对象是否包含指定的String,即"BA"

代码语言:javascript
运行
复制
if(errorEntities!=null) {
    for(ErrorEntity errorEntity : errorEntities){
        for(ErrorItemDetail errorItem : errorEntity.getErrorItem()){
            List<EntityRef> relatedEntity = errorItem.getRelatedEntity();
            if(relatedEntity!=null || !relatedEntity.isEmpty()){
                for(EntityRef entityRef : relatedEntity){
                    return entityRef.getReferredType().equalsIgnoreCase("BA");
                }
            }
        }
    }
}
EN

回答 3

Stack Overflow用户

发布于 2022-04-29 13:03:35

希望这就是你想要的:

代码语言:javascript
运行
复制
if(errorEntities!=null) {
    return errorEntities.stream()
        .map((ErrorEntity errorEntity) -> errorEntity.getErrorItem())
        .flatMap(Collection::stream)
        .map((ErrorItemDetail errorItemDetail) -> errorItemDetail.getRelatedEntity())
        .filter((List<EntityRef> relatedEntity) -> relatedEntity == null || relatedEntity.isEmpty())
        .flatMap(Collection::stream)
        .anyMatch((EntityRef entityRef) -> entityRef.getReferredType().equalsIgnoreCase("BA"));
}
票数 1
EN

Stack Overflow用户

发布于 2022-04-29 13:56:48

我们有三个层次的嵌套列表。在这个层次结构的底部,我们有许多entityRefs列表。我们将从所有这些列表中收集entityRefs到一个单独的流中。然后,我们将查看其中是否有引用类型的"BA":

代码语言:javascript
运行
复制
return errorEntities != null && errorEntities.stream()
        .flatMap(errorEntity -> errorEntity.getErrorItem().stream())
        .map(errorItem -> errorItem.getRelatedEntity())
        .filter(relatedEntity -> relatedEntity != null)
        .flatMap(relatedEntity -> relatedEntity.stream())
        .anyMatch(entityRef -> entityRef.getReferredType().equalsIgnoreCase("BA"))

我们也可以使用方法引用来完成它。在这里,我们将创建一个以大写字母表示的所有引用类型字符串的流。那么我们要看看他们中是否有一个是"BA“

代码语言:javascript
运行
复制
return errorEntities != null && errorEntities.stream()
        .map(ErrorEntity::getErrorItem)
        .flatMap(List::stream)
        .map(ErrorItemDetail::getRelatedEntity)
        .filter(Objects::nonNull)
        .flatMap(List::stream)
        .map(EntityRef::getReferredType)
        .map(String::toUpperCase)
        .anyMatch("BA"::equals));
票数 1
EN

Stack Overflow用户

发布于 2022-04-29 15:36:58

因此,您有一个ErrorEntity列表和一个字符串令牌("BA")。

您需要了解是否至少有一个EntityRef对象嵌套在具有与令牌匹配的属性的ErrorEntity中。

从您列出的代码来判断,您的集合的不同级别上可能存在null值,从干净编码的角度来看,这不是一个好情况。但是您可以使用Java9中可用的静态方法Stream.ofNullable来处理它,它返回一个空流或包含一个元素的流(已经传递给它的参数)。

当您需要从单个流元素和anyMatch()生成一个流时,可以使用anyMatch()来获得一个boolean值。

代码语言:javascript
运行
复制
public static boolean matches(List<ErrorEntity> source, String token) {
    return Stream.ofNullable(source) // Stream<List<ErrorEntity>>
        .flatMap(List::stream)       // Stream<ErrorEntity>
        .flatMap(errorEntity -> errorEntity.getErrorItem().stream())        // Stream<ErrorItemDetail>
        .flatMap(errorItem -> Stream.ofNullable(errorItem.relatedEntity())) // Stream<EntityRef>
        .anyMatch(entityRef -> entityRef.getReferredType().equalsIgnoreCase(token));
}

如果说Java 8不是指"Java-8流“,而是指当前使用的版本为8,那么您可以使用Objects.nonNull()方法进行空检查:

代码语言:javascript
运行
复制
public static boolean matches(List<ErrorEntity> source, String token) {
    return Stream.of(source)                 // Stream<List<ErrorEntity>>
        .filter(Objects::isNull)
        .flatMap(List::stream)               // Stream<ErrorEntity>
        .flatMap(errorEntity -> errorEntity.getErrorItem().stream()) // Stream<ErrorItemDetail>
        .map(ErrorItemDetail::relatedEntity) // Stream<List<EntityRef>>
        .filter(Objects::nonNull)            // Stream<List<EntityRef>>
        .flatMap(List::stream)               // Stream<EntityRef>
        .anyMatch(entityRef -> entityRef.getReferredType().equalsIgnoreCase(token));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72058146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档