如何使用Java 8实现多个循环的迭代?
我需要检查一个嵌套对象是否包含指定的String,即"BA"?
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");
}
}
}
}
}发布于 2022-04-29 13:03:35
希望这就是你想要的:
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"));
}发布于 2022-04-29 13:56:48
我们有三个层次的嵌套列表。在这个层次结构的底部,我们有许多entityRefs列表。我们将从所有这些列表中收集entityRefs到一个单独的流中。然后,我们将查看其中是否有引用类型的"BA":
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“
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));发布于 2022-04-29 15:36:58
因此,您有一个ErrorEntity列表和一个字符串令牌("BA")。
您需要了解是否至少有一个EntityRef对象嵌套在具有与令牌匹配的属性的ErrorEntity中。
从您列出的代码来判断,您的集合的不同级别上可能存在null值,从干净编码的角度来看,这不是一个好情况。但是您可以使用Java9中可用的静态方法Stream.ofNullable来处理它,它返回一个空流或包含一个元素的流(已经传递给它的参数)。
当您需要从单个流元素和anyMatch()生成一个流时,可以使用anyMatch()来获得一个boolean值。
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()方法进行空检查:
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));
}https://stackoverflow.com/questions/72058146
复制相似问题