记一道笔试题目,后台实现Java将Connection<T>集合组装成树(Tree)树结构组装

题目如下:

电商系统的商品分类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":"笔记本"}]}]}]
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

焱童鞋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值