网站建设开发制作苏州优化亚当
news/
2025/9/24 1:56:55/
文章来源:
网站建设开发制作,苏州优化亚当,东莞市南城装饰工程东莞网站建设,在淘宝上的毕设网站代做思路
我们首先要知道数据中#xff0c;哪两列能够体现父子级我们需要找到最顶层父 id 是什么#xff0c;因为只有知道最顶层的父 id#xff0c;我们才能进行递归我们要在不改变数据的原有结构下#xff0c;而转换为 Tree 结构#xff0c;那么就需要创建新的结构
代码
/…思路
我们首先要知道数据中哪两列能够体现父子级我们需要找到最顶层父 id 是什么因为只有知道最顶层的父 id我们才能进行递归我们要在不改变数据的原有结构下而转换为 Tree 结构那么就需要创建新的结构
代码
/*** Date: 2023/10/28* Author: PuKun* Description: 树形数据ID*/
Target(ElementType.FIELD)
Retention(RetentionPolicy.RUNTIME)
public interface TreeId {}/*** Date: 2023/10/28* Author: PuKun* Description: 树形数据的父类id*/
Target(ElementType.FIELD)
Retention(RetentionPolicy.RUNTIME)
public interface TreeParentId {}/*** Date: 2023/10/28* Author: PuKun* Description: Tree工具类*/
public class TreeUtilsT {private ClassT aClass;private Field treeIdField;private Field treeParentIdField;private final String CHILDREN_KEY children;public TreeUtils(ClassT aClass) {if (aClass null) {throw new RuntimeException(传入的class为NULL);}this.aClass aClass;init();}private void init() {Field[] fields this.aClass.getDeclaredFields();for (Field f : fields) {if (f.isAnnotationPresent(TreeId.class)) {this.treeIdField f;} else if (f.isAnnotationPresent(TreeParentId.class)) {this.treeParentIdField f;}// 这一步也没什么用我的想法是如果这两列提前找到那么就提前结束循环if (this.treeIdField ! null this.treeParentIdField ! null) {break;}}if (this.treeIdField null || this.treeParentIdField null) {throw new RuntimeException(没有找到相应的注解);}// 避免因字段为private时无法访问this.treeIdField.setAccessible(true);this.treeParentIdField.setAccessible(true);}public ListHashMapString, Object getTreeData(ListT data) throws IllegalAccessException {Object topParentId getTopParentId(data);return buildTreeData(data, topParentId);}private Object getTopParentId(ListT data) throws IllegalAccessException {// 获取所有parentId的值MapObject, Integer parentIdMap new HashMap();for (T o : data) {Object v this.treeParentIdField.get(o);parentIdMap.put(v, 0);}// 计算每个parentId的数量for (T o : data) {Object v this.treeIdField.get(o);if (parentIdMap.containsKey(v)) {int keyValue parentIdMap.get(v);parentIdMap.put(v, keyValue);}}// 当parentId的数量等于0时说明当前的parentId没有找到对应的treeId那么就是最顶层的parentIdObject topParentId null;SetMap.EntryObject, Integer entries parentIdMap.entrySet();for (Map.EntryObject, Integer e : entries) {if (e.getValue() 0) {topParentId e.getKey();}}return topParentId;}private ListHashMapString, Object buildTreeData(ListT data, Object parentId) throws IllegalAccessException {ListHashMapString, Object list new ArrayList();for (T o : data) {Object pid this.treeParentIdField.get(o);Object tid this.treeIdField.get(o);if (pid.equals(parentId)) {HashMapString, Object map new HashMap();Field[] fields this.aClass.getDeclaredFields();for (Field f : fields) {f.setAccessible(true);map.put(f.getName(), f.get(o));}ListHashMapString, Object children buildTreeData(data, tid);if (children.size() 0) {map.put(CHILDREN_KEY, children);}list.add(map);}}return list;}
}
/*** Date: 2023/11/14* Author: PuKun* Description: 树形工具类测试*/
class TreeUtilsTest {class Dept {TreeIdprivate int id;private String deptName;TreeParentIdprivate int parentId;public Dept(int id, String deptName, int parentId) {this.id id;this.deptName deptName;this.parentId parentId;}public int getId() {return id;}public void setId(int id) {this.id id;}public String getDeptName() {return deptName;}public void setDeptName(String deptName) {this.deptName deptName;}public int getParentId() {return parentId;}public void setParentId(int parentId) {this.parentId parentId;}}class Dept2 {TreeIdprivate String id;private String deptName;TreeParentIdprivate String parentId;public Dept2(String id, String deptName, String parentId) {this.id id;this.deptName deptName;this.parentId parentId;}public String getId() {return id;}public void setId(String id) {this.id id;}public String getDeptName() {return deptName;}public void setDeptName(String deptName) {this.deptName deptName;}public String getParentId() {return parentId;}public void setParentId(String parentId) {this.parentId parentId;}}Testvoid getTreeData() {// ListDept list new ArrayList();
// list.add(new Dept(1, 闲简居有限公司, 0));
// list.add(new Dept(2, 开发部, 1));
// list.add(new Dept(4, Java组, 2));
// list.add(new Dept(5, 前端组, 2));
// list.add(new Dept(3, 测试部, 1));// list.add(new Dept(2, 开发部, 1));
// list.add(new Dept(4, Java组, 2));
// list.add(new Dept(5, 前端组, 2));
// list.add(new Dept(3, 测试部, 1));// TreeUtilsDept treeUtils new TreeUtils(Dept.class);
// ListHashMapString, Object mapList null;
// try {
// mapList treeUtils.getTreeData(list);
// } catch (IllegalAccessException e) {
// Assertions.fail(e.getMessage());
// }ListDept2 list new ArrayList();list.add(new Dept2(1, 闲简居有限公司, 0));list.add(new Dept2(2, 开发部, 1));list.add(new Dept2(4, Java组, 2));list.add(new Dept2(5, 前端组, 2));list.add(new Dept2(3, 测试部, 1));TreeUtilsDept2 treeUtils new TreeUtils(Dept2.class);ListHashMapString, Object mapList null;try {mapList treeUtils.getTreeData(list);} catch (IllegalAccessException e) {Assertions.fail(e.getMessage());}Assertions.assertNotEquals(null, mapList);System.out.println(mapList);}
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/914537.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!