将list数据转换成树型结构
2018-03-13 09:52
351 查看
经常碰到一个功能,是将一个list的数据转换成数据结构,以前每次都是用递归方式进行组装,后来在网上查找了下,看到利用java反射机制封装的一个公用方法,觉得不错,但是网上能搜索到的发现大部分都是利用实体类的属性,调用clazz.getDeclaredField()方法来实现的,但这样有一定的局限性,该方法只能获取到自身的属性,从父类继承的private属性获取不到,于是把代码修改了下,利用反射方法来实现,代码如下/**
* 将list组装成tree
* @param tree 传入的list
* @param topParentId 最顶层的父ID
* @return
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public static List toTree(List tree,String topParentId) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
if (tree !=null){
List t_list = new ArrayList();
Map map = new HashMap();
for (Object o : tree){
Class clazz = o.getClass();
Method method = clazz.getMethod("getId");
String id = method.invoke(o).toString();
map.put(id,o);
}
for (Object o : map.keySet()){
String id = o.toString();
Object obj = map.get(id);
Class clazz = obj.getClass();
Method method = clazz.getMethod("getParentId");
String parentId = method.invoke(obj).toString();
if (StringUtils.pathEquals(parentId,topParentId)){
//顶层
t_list.add(obj);
}else {
//当不是顶层时,获取其父级
Object object = map.get(parentId);
Class parentClazz = object.getClass();
Method getChildren = parentClazz.getMethod("getChildren");
//获取其父级的子级,及自己的兄弟(平级)
List list =(List) getChildren.invoke(object);
if (CollectionUtils.isEmpty(list)){
90f3
list = new ArrayList();
}
list.add(obj);
Method setChildren = parentClazz.getMethod("setChildren",List.class);
setChildren.invoke(object,list);
}
}
return t_list;
}
return null;
}
* 将list组装成tree
* @param tree 传入的list
* @param topParentId 最顶层的父ID
* @return
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public static List toTree(List tree,String topParentId) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
if (tree !=null){
List t_list = new ArrayList();
Map map = new HashMap();
for (Object o : tree){
Class clazz = o.getClass();
Method method = clazz.getMethod("getId");
String id = method.invoke(o).toString();
map.put(id,o);
}
for (Object o : map.keySet()){
String id = o.toString();
Object obj = map.get(id);
Class clazz = obj.getClass();
Method method = clazz.getMethod("getParentId");
String parentId = method.invoke(obj).toString();
if (StringUtils.pathEquals(parentId,topParentId)){
//顶层
t_list.add(obj);
}else {
//当不是顶层时,获取其父级
Object object = map.get(parentId);
Class parentClazz = object.getClass();
Method getChildren = parentClazz.getMethod("getChildren");
//获取其父级的子级,及自己的兄弟(平级)
List list =(List) getChildren.invoke(object);
if (CollectionUtils.isEmpty(list)){
90f3
list = new ArrayList();
}
list.add(obj);
Method setChildren = parentClazz.getMethod("setChildren",List.class);
setChildren.invoke(object,list);
}
}
return t_list;
}
return null;
}
相关文章推荐
- python基础数据结构——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- 数据结构之线性表之顺序表和链表(通过数据结构角度深入理解arrayList和linkedList的特性)
- 数据结构-单链表(Linked List)
- Java中将JSON格式的数据转换成对应的Bean、Map、List数据
- 以树型目录化的数据库表数据通过递归转换成xml格式的xmldoc
- 数据结构——从中缀向后缀转换表达式
- ajax返回值List转换为json格式数据
- 【数据结构与算法】ArrayList和LinkedList
- 数据结构之List
- Java将list数据取出并加入分隔符拼接,转换成String
- 使用LinkedList来模拟一个堆栈或者队列数据结构
- JAVA将ResultSet的结果集转换为List结构
- 以树型目录化的数据库表数据通过递归转换成xml格式的xmldoc
- fastJson在java后台转换json格式数据探究(二)--处理数组/List/Map
- Java核心数据结构(List,Map,Set)使用技巧与优化
- 集合框架体系结构,list、set操作数据,泛型(一)
- 一种理想的在关系数据库中存储树型结构数据的方法
- 【数据结构之List::iterator】leetcode LRUCache
- 数据结构——SimpleArrayList和SimpleLinkedList的实现
- fastJson在java后台转换json格式数据探究(二)--处理数组/List/Map