數(shù)據(jù)庫中存的數(shù)據(jù)結(jié)構(gòu):id和parent_id

image.png
需求:
- 得到一個(gè)樹形結(jié)構(gòu)的json數(shù)據(jù)
- 自定義獲取到某一級(jí),得到樹形結(jié)構(gòu)的json數(shù)據(jù)(有些只創(chuàng)建到了第二級(jí),但想獲得到第四級(jí)的樹,需要將不到第四級(jí)的排除掉)
- 將name按照對(duì)應(yīng)任意層級(jí)拼接(ex:一級(jí)/二級(jí)/三級(jí)/四級(jí))
- 實(shí)現(xiàn)需求一:
//數(shù)據(jù)庫中取出所有l(wèi)ist
List<TreeVo> treeVos = treeMapper.selectTree();
Map<Long, TreeVo> treeVoMap = treeVos.stream()
.collect(Collectors.toMap(TreeVo::getId, e -> e));
treeVos.stream().forEach(e->{
if (treeVoMap.containsKey(e.getParentId())&& e.getParentId() != 0){
TreeVo parentVo = treeVoMap.get(e.getParentId() );
if (parentVo.getChildVOs() == null){
parentVo.setChildVOs(new ArrayList<>());
}
parentVo.getChildVOs().add(e);
}
});
List<TreeVo> collect = treeVoMap.values().stream()
.filter(e -> e.getParentId() == 0)
.collect(Collectors.toList());
- 實(shí)現(xiàn)需求二:
public List<TreeVo> selectValidTree(Integer maxLev){
List<TreeVo> treeVos = treeMapper.selectTree();
Map<Long, TreeVo> treeVoMap = treeVos.stream()
.collect(Collectors.toMap(TreeVo::getId, e -> e));
//得到最后一級(jí)的map(key:父級(jí)id;value:子的集合),依次向上查找
Map<Long, List<TreeVo>> map = new HashMap<>();
for (TreeVo vo: treeVos) {
if (vo.getLevel() == maxLev){
if (!map.containsKey(vo.getParentId())){
map.put(vo.getParentId(),new ArrayList<>());
}
map.get(vo.getParentId()).add(vo);
}
}
if (!map.isEmpty()) {
//通過遞歸向上查找
Map<Long, List<TreeVo>> map2 = recursionFindUp(treeVoMap, map);
return map2.get(0L);
}
return null;
}
/**
* @Description: 篩選層級(jí)不足四級(jí)的視頻分類(從下到上)
* @Param: mapAll:所有數(shù)據(jù)的map,orderMap:結(jié)果map
* @return:
*/
public Map<Long, List<TreeVo>> recursionFindUp(
Map<Long, TreeVo> treeVoMap,
Map<Long, List<TreeVo>> orderMap){
if (orderMap.keySet().stream().findFirst().get()==0 ){//找到了最上級(jí)
return orderMap;
}else {
Map<Long, List<TreeVo>> tempOrderMap = new HashMap<>();
orderMap.forEach((key,value)->{
if (treeVoMap.containsKey(key)){
//找到父級(jí)
TreeVo allVO = treeVoMap.get(key);
//將子的集合放到此父級(jí)中
allVO.setChildVOs(value);
//找到父級(jí)的父級(jí)
if (!tempOrderMap.containsKey(allVO.getParentId())){
tempOrderMap.put(allVO.getParentId(),new ArrayList<>());
}
tempOrderMap.get(allVO.getParentId()).add(allVO);
}
});
return recursionFindUp(treeVoMap,tempOrderMap);
}
}
結(jié)果與需求一差不多,只是排除了不夠級(jí)別的數(shù)據(jù)
- 實(shí)現(xiàn)需求三:
/**
* @Description: 得到視頻分類的選項(xiàng)列表
* @Param: type 視頻分類類別,maxLev查詢的最大級(jí)別
* @return:
*/
public List<TreeOptionVo> getOptionsList( Integer maxLev) {
//@Select("select id , parent_id AS 'parentId' , name , level , del from rv_video_category where del=0 and level <= #{maxLev}")
List<TreeVo> treeVos = treeMapper.selectTreeOptionByMaxLev(maxLev);
Map<Long, TreeVo> treeVoMap = treeVos.stream().collect(Collectors.toMap(TreeVo::getId, e -> e));
treeVos.stream().forEach(e->{
if (treeVoMap.containsKey(e.getParentId()) && e.getParentId() != 0){
TreeVo parentVo = treeVoMap.get(e.getParentId() );
if (parentVo.getChildVOs() == null){
parentVo.setChildVOs(new ArrayList<>());
}
parentVo.getChildVOs().add(e);
}
});
List<TreeVo> collect = treeVoMap.values().stream().filter(e -> e.getParentId() == 0).collect(Collectors.toList());
//以上跟實(shí)現(xiàn)一步驟一樣,搜索條件增加了一個(gè)level <= #{maxLev}。只是沒單獨(dú)抽離出來
List<TreeOptionVo> list = new ArrayList<>();
for (TreeVo vo : collect) {
TreeOptionVo optionVO = new TreeOptionVo();
optionVO.setName(vo.getName());
optionVO.setMaxLevel(vo.getLevel());
treeOption(vo.getChildVOs(), list, optionVO, maxLev);
}
return list;
}
/**
* @Description: 通過拼接得到對(duì)象的測試方法
* @Param:
* @return:
*/
public void treeOption(
List<TreeVo> allVOS,
List<TreeOptionVo> list,
TreeOptionVo node, Integer maxLev) {
if (allVOS == null) {
if (node.getMaxLevel().equals(maxLev)) {
list.add(node);
}
} else {
for (TreeVo vo : allVOS) {
Integer currentLevel = node.getMaxLevel();
if (vo.getChildVOs() == null) {
if (currentLevel == maxLev - 1) {
TreeOptionVo optionVO = new TreeOptionVo();
optionVO.setName(node.getName().concat("/" + vo.getName()));
list.add(optionVO);
}
} else {
TreeOptionVo temp = new TreeOptionVo();
temp.setName(node.getName().concat("/" + vo.getName()));
temp.setMaxLevel(vo.getLevel());
treeOption(vo.getChildVOs(), list, temp, maxLev);
}
}
}
}
結(jié)果:

image.png