题目如下:
电商系统的商品分类Category一般分多级, 比如运动户外->运动服饰->运动内衣,假设
Category.java如下:
class Category {
private Long id;
private String name;
//上级分类id
private Long parentId;
}
Category.java只有上级id的引用, 我们需要一个树状数据结构CategoryDto.java, 如下:
class CategoryDto {
private Long id;
private String name;
//下级分类
private Collection<CategoryDto> subs;
}
问题: 现有全部分类Collection<Category>, 转换成树状数据结构:
实现Collection<CategoryDto> buildCategoryTree(Collection<Category> allCategories); 可使用伪代码
[
{
"id": 1,
"name": "运动户外",
"subs": [
{
"id": 2,
"name": "运动服饰",
"subs": [
{
"id": 3,
"name": "运动内衣"
},
{
"id": 4,
"name": "运动配饰"
}
]
}
]
},
{
"id": 5,
"name": "电脑",
"subs": [
{
"id": 6,
"name": "电脑整机",
"subs": [
{
"id": 7,
"name": "笔记本"
}
]
}
]
}
]
1.Category原始bean对象:
public class Category {
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + ", parentId=" + parentId + ", getId()=" + getId()
+ ", getName()=" + getName() + ", getParentId()=" + getParentId() +"]";
}
private Long id;
private String name;
//上级分类id
private Long parentId;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getParentId() {
return parentId;
}
public void setParentId(Long parentId) {
this.parentId = parentId;
}
public Category(Long id, String name, Long parentId) {
this.id = id;
this.name = name;
this.parentId = parentId;
}
}
2.dto对象:
package exam;
import java.util.Collection;
public class CategoryDto {
private Long id;
private String name;
//下级分类
private Collection<CategoryDto> subs;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<CategoryDto> getSubs() {
return subs;
}
public void setSubs(Collection<CategoryDto> subs) {
this.subs = subs;
}
}
3.测试类:
package exam;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
/**
* @类描述:
* @项目名称:test
* @包名: exam
* @类名称:ChangeTest
* @创建人:dxy
* @创建时间:2019年6月25日上午11:36:34
* @修改人:dxy
* @修改时间:2019年6月25日上午11:36:34
* @修改备注:
* @version v1.0
* @see
* @bug
* @邮箱 787455069@qq.com
*/
public class ChangeTest {
public static void main(String[] args) {
List<Category> allCategorislist=new ArrayList<Category>();
allCategorislist.add(new Category(3L, "运动内衣", 2L));
allCategorislist.add(new Category(4L, "运动配饰", 2L));
allCategorislist.add(new Category(2L, "运动服饰", 1L));
allCategorislist.add(new Category(1L, "运动户外", 0L));//pid =0 定义为顶级分类
allCategorislist.add(new Category(5L, "电脑", 0L));
allCategorislist.add(new Category(6L, "电脑整机", 5L));
allCategorislist.add(new Category(7L, "笔记本", 6L));
System.out.println("转化前:"+JSON.toJSONString(allCategorislist));
Collection<Category> allCategories =allCategorislist;
Collection<CategoryDto> buildCategoryTree = ChangeTest.buildCategoryTree(allCategories);
System.out.println("转化后:"+JSON.toJSONString(buildCategoryTree));
}
public static Collection<CategoryDto> buildCategoryTree(Collection<Category> allCategories){
List<CategoryDto> result=new ArrayList<CategoryDto>();
List<Category> c=new ArrayList<Category>();
c=(List<Category>) allCategories;
Map <Long,Category> temp=new HashMap<Long, Category>();
//遍历结果集 -->移入目标map k - v
for(int i=0;i<c.size();i++){
temp.put(c.get(i).getId(), c.get(i));
}
//遍历结果集
for(int j=0;j< c.size();j++){
//单条记录
Category cx=c.get(j);
//CategoryDto
temp.get(cx.getParentId());
if(temp.get(cx.getParentId())==null) {//获取根节点
CategoryDto ct =new CategoryDto();
ct.setId(cx.getId());
ct.setName(cx.getName());
ct.setSubs(foreachNOde(allCategories,cx.getId()));
result.add(ct);
}
}
return result;
}
public static Collection<CategoryDto> foreachNOde(Collection<Category> allCategories,Long pid){
List<CategoryDto> result=new ArrayList<CategoryDto>();
List<Category> result1=new ArrayList<Category>();
result1= (List<Category>) allCategories;
for (int i = 0; i < result1.size(); i++) {
if(pid==result1.get(i).getParentId()) {
CategoryDto cc=new CategoryDto();
cc.setId(result1.get(i).getId());
cc.setName(result1.get(i).getName());
if(foreachNOde(allCategories,result1.get(i).getId()).size()!=0) {//有subs值
cc.setSubs(foreachNOde(allCategories,result1.get(i).getId()));
}
result.add(cc);
}
}
return result;
}
}
4.结果:
转化前:[{"id":3,"name":"运动内衣","parentId":2},{"id":4,"name":"运动配饰","parentId":2},{"id":2,"name":"运动服饰","parentId":1},{"id":1,"name":"运动户外","parentId":0},{"id":5,"name":"电脑","parentId":0},{"id":6,"name":"电脑整机","parentId":5},{"id":7,"name":"笔记本","parentId":6}]
转化后:[{"id":1,"name":"运动户外","subs":[{"id":2,"name":"运动服饰","subs":[{"id":3,"name":"运动内衣"},{"id":4,"name":"运动配饰"}]}]},{"id":5,"name":"电脑","subs":[{"id":6,"name":"电脑整机","subs":[{"id":7,"name":"笔记本"}]}]}]