public class Tools{
public static void main(String[] args) {//测试用例
TreeNodes keys = new TreeNodes ("ptId", "parentId", "ptName", "ptLevels");
//String str = "[{\"parentId\":\"0\",\"ptDeleted\":\"0\",\"ptId\":\"1\",\"ptLevels\":1,\"ptName\":\"类型1\"},{\"parentId\":\"0\",\"ptDeleted\":\"0\",\"ptId\":\"2\",\"ptLevels\":1,\"ptName\":\"类型2\"},{\"parentId\":\"2\",\"ptDeleted\":\"0\",\"ptId\":\"3\",\"ptLevels\":2,\"ptName\":\"类型23\"}]";
//String s1 = changeKey(str, keys);
//System.out.println(s1);
List> pts = getListMap();//模拟获取列表信息
List list = Tools.formatTree(pts,keys);//将列表转换为树
System.out.println(JSON.toJSONString(list));
}
public static List> getListMap(){
List> list = new ArrayList>();
Map map1 = new HashMap();
map1.put("ptId",1); map1.put("parentId",0); map1.put("ptLevels",1);map1.put("ptName","测试用例1");list.add(map1);
Map map2 = new HashMap();
map2.put("ptId",2); map2.put("parentId",0); map2.put("ptLevels",1);map2.put("ptName","测试用例2");list.add(map2);
Map map3 = new HashMap();
map3.put("ptId",3); map3.put("parentId",0); map3.put("ptLevels",1);map3.put("ptName","测试用例3");list.add(map3);
Map map4 = new HashMap();
map4.put("ptId",4); map4.put("parentId",1); map4.put("ptLevels",2);map4.put("ptName","测试用例4");list.add(map4);
Map map5 = new HashMap();
map5.put("ptId",5); map5.put("parentId",2); map5.put("ptLevels",2);map5.put("ptName","测试用例5");list.add(map5);
Map map6 = new HashMap();
map6.put("ptId",6); map6.put("parentId",3); map6.put("ptLevels",2);map6.put("ptName","测试用例6");list.add(map6);
Map map7 = new HashMap();
map7.put("ptId",7); map7.put("parentId",4); map7.put("ptLevels",3);map7.put("ptName","测试用例7");list.add(map7);
Map map8 = new HashMap();
map8.put("ptId",8); map8.put("parentId",5); map8.put("ptLevels",3);map8.put("ptName","测试用例8");list.add(map8);
return list;
}
/** * @param list 树的内容,需要按照level从小到大进行排序,否则无法获取到第一个层级,且易造成树的分支丢失 * @param keys * @param * @return */
public static List formatTree(List list, TreeNodes keys) {
if (list &#61;&#61; null || list.size() <&#61; 0) {
return new ArrayList<>();
}
String jsonStr &#61; JSON.toJSONString(list);
jsonStr &#61; changeKey(jsonStr, keys);//change&#xff0c;不改变格式&#xff0c;不需要这段代码&#xff0c;直接删除
List treeNodes &#61; listClone(jsonStr, TreeNodes.class);//change&#xff0c;不改变格式&#xff0c;不需要这段代码&#xff0c;直接删除
Map> map &#61; new HashMap<>();//change&#xff0c;修改List为指定的类型
//转换字段
List root &#61; null;//根目录 //change&#xff0c;修改List为指定的类型
for (TreeNodes current : treeNodes) {//change&#xff0c;修改List为指定的类型
{//添加当前元素到指定级别
String level &#61; current.getLevel();//change&#xff0c;修改获取层级的方法
if (!map.containsKey(level)) {//不存在&#xff0c;先添加list
map.put(level, new ArrayList());//change&#xff0c;修改List为指定的类型
}
List arr &#61; map.get(level);//当前层级//change&#xff0c;修改List为指定的类型
arr.add(current);
if (root &#61;&#61; null) {//表示是第一级
root &#61; arr;
}
}
//将当前元素添加到父级的子元素列表里
{
List parentTree &#61; map.get(String.valueOf(Integer.valueOf(current.getLevel()) - 1));//change&#xff0c;修改List、获取层级的方法
if (parentTree &#61;&#61; null) {
continue;
}
for (TreeNodes parent : parentTree) {//change&#xff0c;修改List为指定的类型
if (parent.getId().equals(current.getpId())) {//如果找不到父级&#xff0c;则为异常数据&#xff0c;抛弃 //change&#xff0c;修改上下级关联的判断依据
parent.getChildren().add(current);
break;
}
}
}
}
return root;
}
/** * 更新字段 */
public static String changeKey(String json, TreeNodes keys) {
if (StringUtils.isEmpty(json) || keys &#61;&#61; null) {
return "";
}
json &#61; json.replaceAll(keys.getId(), "id")
.replaceAll(keys.getText(), "text")
.replaceAll(keys.getpId(), "pId")
.replaceAll(keys.getLevel(), "level");
return json;
}
/** * 将一个对象的列表转换为另一个对象的列表 * * &#64;param k * &#64;param clazz * &#64;param * &#64;param * &#64;return */
public static List listClone(List k, Class clazz) {
List list &#61; JSONArray.parseArray((JSONArray.toJSON(k)).toString(), clazz);
return list;
}
/** * 将json格式的字符串转换为指定对象的列表 * * &#64;param str * &#64;param clazz * &#64;param * &#64;return */
public static List listClone(String str,Class clazz){
List list &#61; JSONArray.parseArray(str, clazz);
return list;
}
}