将所有类别的列表拆分为子类别?(仅使用Java)

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

Split all list of all categories into subcategories? (with Java only)

问题

我们有一个实体类以及一个包含所有类别的列表:

class Category {
  Long id, parentId;
}
...
List<Category> categoryList = <...>;

如何转换为一个类似这样的DTO列表:

class CategoryDTO {
  Long id; 
  List<CategoryDTO> subcategories;
}

在没有一对一实体关系的情况下如何实现这个转换?

英文:

We have an entity class and a list of all categories:

class Category {
  Long id, parentId;
}
...
List&lt;Category&gt; categoryList = &lt;...&gt;;

How to convert to a list of DTOs like this:

class CategoryDTO {
  Long id; 
  List&lt;CategoryDTO&gt; subcategories;
}

How can this be done without a One-to-One entity relationship?

答案1

得分: 2

public class CategoryConverterImpl implements CategoryConverter {
    private CategoryDTO convertEntity(Category s) {
        Long id = s.getId();
        return new CategoryDTO()
                .setId(id)
                .setSubcategories(
                        convertCollection(
                                categoryCollection.stream()
                                        .filter(c -> Objects.equals(c.getParentCategoryId(), id))
                                        .collect(Collectors.toList())
                        )
                );
    }

    private List<CategoryDTO> convertCollection(Collection<Category> categoryCollection) {
        return categoryCollection.stream()
                .map(this::convertEntity)
                .collect(Collectors.toList());
    }

    private Collection<Category> categoryCollection;

    @Override
    public List<CategoryDTO> convert(Collection<Category> categoryCollection) {
        this.categoryCollection = categoryCollection;
        return categoryCollection.stream()
                .filter(c -> c.getParentCategoryId() == null)
                .map(this::convertEntity)
                .collect(Collectors.toList());
    }
}
英文:
public class CategoryConverterImpl implements CategoryConverter {
    private CategoryDTO convertEntity(Category s) {
        Long id = s.getId();
        return new CategoryDTO()
                .setId(id)
                .setSubcategories(
                        convertCollection(
                                categoryCollection.stream()
                                        .filter(c -&gt; Objects.equals(c.getParentCategoryId(), id))
                                        .collect(Collectors.toList())
                        )
                );
    }

    private List&lt;CategoryDTO&gt; convertCollection(Collection&lt;Category&gt; categoryCollection) {
        return categoryCollection.stream()
                .map(this::convertEntity)
                .collect(Collectors.toList());
    }

    private Collection&lt;Category&gt; categoryCollection;

    @Override
    public List&lt;CategoryDTO&gt; convert(Collection&lt;Category&gt; categoryCollection) {
        this.categoryCollection = categoryCollection;
        return categoryCollection.stream()
                .filter(c -&gt; c.getParentCategoryId() == null)
                .map(this::convertEntity)
                .collect(Collectors.toList());
    }
}

答案2

得分: 1

创建一个 Map<Long, CategoryDTO>,并从 categoryList 中填充数据,将 id 映射到从 Category 对象创建的 CategoryDTO

然后再次遍历 categoryList,在映射中查找 idparentId,并根据情况添加到 subcategories 列表中。

例如,类似于以下内容:

Map<Long, CategoryDTO> categoryDTOs = new LinkedHashMap<>();
for (Category category : categoryList) {
    categoryDTOs.put(category.getId(), new CategoryDTO(category.getId()));
}
for (Category category : categoryList) {
    if (category.getParentId() != null) {
        categoryDTOs.get(category.getParentId())
                    .addSubcategory(categoryDTOs.get(category.getId()));
    }
}
英文:

Create a Map&lt;Long, CategoryDTO&gt; and fill it from categoryList, mapping id to a CategoryDTO created from the Category object.

Then loop through categoryList again, lookup both id and parentId in the map, and add to the subcategories list as appropriate.

E.g. something like this:

Map&lt;Long, CategoryDTO&gt; categoryDTOs = new LinkedHashMap&lt;&gt;();
for (Category category : categoryList) {
	categoryDTOs.put(category.getId(), new CategoryDTO(category.getId()));
}
for (Category category : categoryList) {
	if (category.getParentId() != null) {
		categoryDTOs.get(category.getParentId())
		            .addSubcategory(categoryDTOs.get(category.getId()));
	}
}

huangapple
  • 本文由 发表于 2020年9月6日 03:56:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63757972.html
匿名

发表评论

匿名网友

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

确定