package com.byt.finance.util;
import javax.validation.constraints.NotNull;
import java.util.*;
/**
* @description:
*/
public class TreeUtils<V> {
private List<V> selectList;
private List<V> allDataList;
private DataHelper<V> dataHelper;
/**
* 提供多参构造方法
* @param selectList 选中的或者搜索出来的
* @param allDataList
* @param dataHelper
*/
public TreeUtils(List<V> selectList, @NotNull List<V> allDataList, DataHelper<V> dataHelper){
this.selectList = selectList;
this.allDataList = allDataList;
this.dataHelper = dataHelper;
}
public List<V> convertTree(){
//如果选中或者查询结果是空 直接返回
if(selectList == null || selectList.size() == 0){
return selectList;
}
Map<String, V> allDataMap = new LinkedHashMap<>();
//list转存map 这里的list是所有数据的list
for(V v: allDataList){
String key = dataHelper.getKey(v);
if(null == key){
System.out.println("检测到存在key为空的数据,程序即将退出....");
break;
}
allDataMap.put(dataHelper.getKey(v), v);
}
//转换前执行的操作 如果有需要的话
if(dataHelper != null){
dataHelper.before(allDataList, allDataMap);
}
Map<String, V> retMap = new LinkedHashMap<>();
for (V item : selectList) {
updateItemInfo(allDataMap, item, retMap);
}
List<V> result = new ArrayList<>(retMap.values());
if(dataHelper != null){
dataHelper.after(result);
}
return result;
}
/**
* 更新节点信息
* @param allDataMap
* @param item
* @param retMap
*/
private void updateItemInfo(Map<String,V> allDataMap, V item, Map<String,V> retMap) {
V v = allDataMap.get(dataHelper.getKey(item));
if(v == null){
System.out.println("allDataMap中获取指定key的数据返回空: " + dataHelper.getKey(item));
return;
}
//判断是不是顶级节点
String pid = dataHelper.getParentKey(item);
if (pid == null || "0".equals(pid)) {
//是顶级节点直接放到结果map里
retMap.put(dataHelper.getKey(item), v);
return;
}
//不是顶级节点-找到它的父
V parent = allDataMap.get(pid);
if(parent == null){
System.out.println("allDataMap中获取指定key-pid的数据返回空: " + pid);
System.out.println("异常数据信息:" + v);
return;
}
//找到它的所有子
List<V> children = dataHelper.getChildren(parent);
if (children == null) {
children = new LinkedList<>();
}
//替换指定元素
if(children.size() > 0){
int index = -1;
for (int i = 0; i < children.size(); i++) {
if(dataHelper.getKey(children.get(i)).equals(dataHelper.getKey(item))){
index = i;
break;
}
}
if(index != -1){
children.remove(index);
children.add(index, v);
}else {
children.add(v);
}
} else {
children.add(v);
}
dataHelper.setChildren(children, parent);
allDataMap.put(pid, parent);
updateItemInfo(allDataMap, parent, retMap);
}
public interface DataHelper<V> {
/**
* 转换之前的数据处理
* @param srcData
* @param srcMap
*/
void before(List<V> srcData, Map<String, V> srcMap);
/**
* 转换之后的数据处理
* @param resultList
*/
void after(Collection<V> resultList);
/**
* 获取主键
* @param item
* @return
*/
String getKey(V item);
/**
* 获取父键
* @param item
* @return
*/
String getParentKey(V item);
/**
* 获取所有的子
* @param item
* @return
*/
List<V> getChildren(V item);
/**
* 重新设置子
* @param children
* @param item
*/
void setChildren(List<V> children, V item);
}
}
package com.byt.finance.util;
import com.byt.finance.vo.SSubjectMenuVO;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* @title MenuDataHelper
* @description
* @date
*/
public class MenuDataHelper implements TreeUtils.DataHelper<SSubjectMenuVO> {
@Override
public void before(List<SSubjectMenuVO> srcData, Map<String, SSubjectMenuVO> srcMap) {
}
@Override
public void after(Collection<SSubjectMenuVO> resultList) {
}
@Override
public String getKey(SSubjectMenuVO item) {
return item.getSubjectId().toString();
}
@Override
public String getParentKey(SSubjectMenuVO item) {
return item.getSubjectParentId().toString();
}
@Override
public List<SSubjectMenuVO> getChildren(SSubjectMenuVO item) {
return item.getChildSubject();
}
@Override
public void setChildren(List<SSubjectMenuVO> children, SSubjectMenuVO item) {
item.setChildSubject(children);
}
}
java list转树结构
最新推荐文章于 2024-08-15 14:47:41 发布