Java 8:递归流映射

huangapple go评论69阅读模式
英文:

Java 8: Recursive stream map

问题

在我的 Library 类中有一个 Set<Library> items 字段。因此在 LibraryDTO 中我有 Set<LibraryDTO> items。当我从 LibraryDTO 构建一个 Library 时,我意识到在将DTO转换为实体的这个items字段中会进入无限循环,例如:

private Library buildLibraryFromLibraryDTO(LibraryDTO libraryDTO) {
    return Library.builder()
        .items(libraryDTO.getItems().stream().map(libraryDTOItem -> {
            Library itemEntity = buildLibraryFromLibraryDTO(libraryDTOItem);
            // Additional conversion logic for other fields
            return itemEntity;
        }).collect(Collectors.toSet()))
        .build();
}

我考虑过编写一个递归方法,通过items流进行遍历,但在逻辑方面遇到了困难。我如何处理这种情况?我进行了研究,看到了一些使用流进行递归的示例,但无法为我的情况复制这些示例。谢谢。

英文:

In my Library class there is a Set<Library> items field. Hence there in the LibraryDTO I have Set<LibraryDTO> items. When I build a Library from the LibraryDTO, I realized that I will enter an infinite loop in converting the DTO to an entity in this items field, example:

private Library buildLibraryFromLibraryDTO(LibraryDTO libraryDTO) {

return Library.builder()		
		.items(libraryDTO.getItems().stream().map(library -> Library.builder()						
				.items(library.getItems().stream().map ... repeats the same DTO to entity conversion process from the above level)					
				.build()).collect(Collectors.toSet()))

		.build();
}

I thought about making a recursive method going through the items stream but I'm having difficulties with logic. How could I do it? I researched and saw some examples of recursion with stream but was unable to replicate for my case. Thank you

答案1

得分: 2

你需要在不需要递归调用时使用基本情况,以避免无限循环。这意味着当 libraryDTO.getItems() 为 null 时,不要再次调用。

你可以使用三元运算符来检查 libraryDTO.getItems() 是否不为 null,然后为每个项目调用 buildLibraryFromLibraryDTO,否则返回 null。

private Library buildLibraryFromLibraryDTO(LibraryDTO libraryDTO) {
    return Library.builder()
            .items(libraryDTO.getItems() != null 
                           ? libraryDTO.getItems()
                             .stream()
                             .map(library -> buildLibraryFromLibraryDTO(library))
                             .collect(Collectors.toSet())
                           : null)
            .build();
}
英文:

You need to use a base case when you don't need to call recursively to avoid an infinite loop. Means when libraryDTO.getItems() is null then don't call again.

You can use ternary operator to check if libraryDTO.getItems() is not null then call buildLibraryFromLibraryDTO for every item else null.

private Library buildLibraryFromLibraryDTO(LibraryDTO libraryDTO) {
return Library.builder()
        .items(libraryDTO.getItems() != null 
                       ? libraryDTO.getItems()
                         .stream()
                         .map(library -> buildLibraryFromLibraryDTO(library))
                         .collect(Collectors.toSet())
                       : null)
        .build();
}

huangapple
  • 本文由 发表于 2020年9月4日 23:33:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63744119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定