英文:
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<Category> categoryList = <...>;
How to convert to a list of DTOs like this:
class CategoryDTO {
Long id;
List<CategoryDTO> 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 -> 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());
}
}
答案2
得分: 1
创建一个 Map<Long, CategoryDTO>
,并从 categoryList
中填充数据,将 id
映射到从 Category
对象创建的 CategoryDTO
。
然后再次遍历 categoryList
,在映射中查找 id
和 parentId
,并根据情况添加到 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<Long, CategoryDTO>
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<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()));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论