本文主要是介绍java树形结构,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.sie.sdap.comm.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TreeUtil {
public static List<HashMap<String, Object>> listToTree(JSONArray arr, String id, String pid, String child) {
JSONArray r = new JSONArray();
JSONObject hash = new JSONObject();
//将数组转为Object的形式,key为数组中的id
for (int i = 0; i < arr.size(); i++) {
JSONObject json = (JSONObject) arr.get(i);
hash.put(json.getString(id), json);
}
//遍历结果集
for (int j = 0; j < arr.size(); j++) {
//单条记录
JSONObject aVal = (JSONObject) arr.get(j);
if(StringUtils.isEmpty(aVal.get(pid))){
aVal.put(pid,null);
}
//在hash中取出key为单条记录中pid的值
JSONObject hashVP = aVal.get(pid) != null ? (JSONObject) hash.get(aVal.get(pid).toString()) : null;
//如果记录的pid存在,则说明它有父节点,将她添加到孩子节点的集合中
if (hashVP != null) {
//检查是否有child属性
if (hashVP.get(child) != null) {
JSONArray ch = (JSONArray) hashVP.get(child);
ch.add(aVal);
hashVP.put(child, ch);
} else {
JSONArray ch = new JSONArray();
ch.add(aVal);
hashVP.put(child, ch);
}
} else {
r.add(aVal);
}
}
return JsonUtils.toListMap(JsonUtils.toStringNoFilteNull(r));
}
/**
* @param dataList
* @param parentName
* @param parentChildrenColumn
* @return
*/
public static List<HashMap<String, Object>> getTreeToParentChildren(List<HashMap<String, Object>> dataList, String parentName, String parentChildrenColumn) {
List<HashMap<String, Object>> treeDataList = new ArrayList<HashMap<String, Object>>() {{
add(new HashMap<String, Object>() {{
put("id", parentName);
put("pId", "0");
put("name", parentName);
}});
}};
String[] parentChildrenColumns = StringUtils.toArray(parentChildrenColumn);//获取字段
for (HashMap<String, Object> dataMap : dataList) {
for (int i = 0; i < parentChildrenColumns.length; i++) {
String id = "";
String pId = "";
String name = StringUtils.isNotEmpty(dataMap.get(parentChildrenColumns[i])) ? dataMap.get(parentChildrenColumns[i]).toString():"";
if(StringUtils.isNotEmpty(name)) {
List<String> pColumnList = new ArrayList<String>();
List<String> columnList = new ArrayList<String>();
for (int x = 0; x <= i; x++) {
columnList.add(parentChildrenColumns[x]);
if (x != i) {
pColumnList.add(parentChildrenColumns[x]);
}
}
id = StringUtils.getStrVal(dataMap, columnList, false, "~");
pId = i == 0 ? parentName : StringUtils.getStrVal(dataMap, pColumnList, false, "~");
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
map.put("pId", pId);
map.put("name", name);
treeDataList.add(map);
}
}
}
treeDataList = ListUtils.removeRepeatMapByKey(treeDataList, "id");
return listToTree(JsonUtils.parseArray(treeDataList), "id", "pId", "children");
}
;
public static void main(String[] args) {
List<HashMap<String,Object>> data = new ArrayList<>();
HashMap<String,Object> map = new HashMap<>();
map.put("id","1");
map.put("pid","0");
map.put("name","甘肃省");
data.add(map);
HashMap<String,Object> map2 = new HashMap<>();
map2.put("id","2");
map2.put("pid","1");
map2.put("name","天水市");
data.add(map2);
HashMap<String,Object> map22 = new HashMap<>();
map22.put("id","22");
map22.put("pid","1");
map22.put("name","cs市");
data.add(map22);
HashMap<String,Object> map3 = new HashMap<>();
map3.put("id","3");
map3.put("pid","2");
map3.put("name","秦州区");
data.add(map3);
HashMap<String,Object> map4 = new HashMap<>();
map4.put("id","4");
map4.put("pid","0");
map4.put("name","北京市");
data.add(map4);
HashMap<String,Object> map5 = new HashMap<>();
map5.put("id","5");
map5.put("pid","4");
map5.put("name","昌平区");
data.add(map5);
System.out.println("data"+JSON.toJSONString(data));
List<HashMap<String, Object>> result = listToTree(JsonUtils.parseArray(data),"id","pid","children");
System.out.println("result"+JSON.toJSONString(result));
/*String jsonStr = "[{\"ID\":\"014c650ef4be3c9d805fa588b869a475\",\"LABEL_OBJECT\":\"用户\",\"LABEL_NAME\":\"网站\",\"LABEL_TYPE1\":\"用户\",\"LABEL_TYPE2\":\"偏好\",\"LABEL_TYPE3\":\"搜索网站类型\",\"LABEL_TYPE4\":\"属性\",\"LABEL_TYPE\":\"统计型\",\"LABEL_COMMENT\":\"\",\"LABEL_OBJECT_CODE\":\"01\",\"LABEL_ID\":\"01010010020010001\",\"LABEL_CODE1\":\"01\",\"LABEL_CODE2\":\"01001\",\"LABEL_CODE3\":\"01001002\",\"LABEL_CODE4\":\"01001002001\",\"LABEL_SQL\":\"\",\"IS_CONFIG\":\"0\",\"IS_VALID\":\"1\",\"UPDATE_FREQ\":\"\",\"TENANT_ID\":\"\",\"CREATION_DATE\":\"\",\"CREATED_BY\":\"\",\"LAST_UPDATE_DATE\":\"\",\"LAST_UPDATED_BY\":\"\",\"LAST_UPDATE_LOGIN\":\"\",\"DELETE_FLAG\":\"0\",\"VERSION_NUM\":\"0\"},{\"ID\":\"0d137c01f62305e00da6773ec3a16319\",\"LABEL_OBJECT\":\"用户\",\"LABEL_NAME\":\"下订单网站数量一般\",\"LABEL_TYPE1\":\"店铺\",\"LABEL_TYPE2\":\"下订单网站数量\",\"LABEL_TYPE3\":\"下订单网站数量\",\"LABEL_TYPE4\":\"行为\",\"LABEL_TYPE\":\"规则型\",\"LABEL_COMMENT\":\"\",\"LABEL_OBJECT_CODE\":\"01\",\"LABEL_ID\":\"01030010010030001\",\"LABEL_CODE1\":\"03\",\"LABEL_CODE2\":\"03001\",\"LABEL_CODE3\":\"03001001\",\"LABEL_CODE4\":\"03001001003\",\"LABEL_SQL\":\"\",\"IS_CONFIG\":\"0\",\"IS_VALID\":\"1\",\"UPDATE_FREQ\":\"\",\"TENANT_ID\":\"\",\"CREATION_DATE\":\"\",\"CREATED_BY\":\"\",\"LAST_UPDATE_DATE\":\"\",\"LAST_UPDATED_BY\":\"\",\"LAST_UPDATE_LOGIN\":\"\",\"DELETE_FLAG\":\"0\",\"VERSION_NUM\":\"0\"},{\"ID\":\"0d89e9b8d3b87ee9d16b53c3c947407d\",\"LABEL_OBJECT\":\"用户\",\"LABEL_NAME\":\"下订单网站数量多\",\"LABEL_TYPE1\":\"店铺\",\"LABEL_TYPE2\":\"下订单网站数量\",\"LABEL_TYPE3\":\"下订单网站数量\",\"LABEL_TYPE4\":\"行为\",\"LABEL_TYPE\":\"规则型\",\"LABEL_COMMENT\":\"\",\"LABEL_OBJECT_CODE\":\"01\",\"LABEL_ID\":\"01030010010030002\",\"LABEL_CODE1\":\"03\",\"LABEL_CODE2\":\"03001\",\"LABEL_CODE3\":\"03001001\",\"LABEL_CODE4\":\"03001001003\",\"LABEL_SQL\":\"\",\"IS_CONFIG\":\"0\",\"IS_VALID\":\"1\",\"UPDATE_FREQ\":\"\",\"TENANT_ID\":\"\",\"CREATION_DATE\":\"\",\"CREATED_BY\":\"\",\"LAST_UPDATE_DATE\":\"\",\"LAST_UPDATED_BY\":\"\",\"LAST_UPDATE_LOGIN\":\"\",\"DELETE_FLAG\":\"0\",\"VERSION_NUM\":\"0\"},{\"ID\":\"0e3b892d4203ec527274e8b0465dadd0\",\"LABEL_OBJECT\":\"用户\",\"LABEL_NAME\":\"下订单网站数量少\",\"LABEL_TYPE1\":\"店铺\",\"LABEL_TYPE2\":\"下订单网站数量\",\"LABEL_TYPE3\":\"下订单网站数量\",\"LABEL_TYPE4\":\"行为\",\"LABEL_TYPE\":\"规则型\",\"LABEL_COMMENT\":\"\",\"LABEL_OBJECT_CODE\":\"01\",\"LABEL_ID\":\"01030010010030003\",\"LABEL_CODE1\":\"03\",\"LABEL_CODE2\":\"03001\",\"LABEL_CODE3\":\"03001001\",\"LABEL_CODE4\":\"03001001003\",\"LABEL_SQL\":\"\",\"IS_CONFIG\":\"0\",\"IS_VALID\":\"1\",\"UPDATE_FREQ\":\"\",\"TENANT_ID\":\"\",\"CREATION_DATE\":\"\",\"CREATED_BY\":\"\",\"LAST_UPDATE_DATE\":\"\",\"LAST_UPDATED_BY\":\"\",\"LAST_UPDATE_LOGIN\":\"\",\"DELETE_FLAG\":\"0\",\"VERSION_NUM\":\"0\"},{\"ID\":\"56621513fc963c9c44de28139713cd31\",\"LABEL_OBJECT\":\"渠道\",\"LABEL_NAME\":\"网站\",\"LABEL_TYPE1\":\"渠道\",\"LABEL_TYPE2\":\"渠道类型\",\"LABEL_TYPE3\":\"渠道类型\",\"LABEL_TYPE4\":\"属性\",\"LABEL_TYPE\":\"统计型\",\"LABEL_COMMENT\":\"\",\"LABEL_OBJECT_CODE\":\"05\",\"LABEL_ID\":\"05050020010010002\",\"LABEL_CODE1\":\"05\",\"LABEL_CODE2\":\"05002\",\"LABEL_CODE3\":\"05002001\",\"LABEL_CODE4\":\"05002001001\",\"LABEL_SQL\":\"\",\"IS_CONFIG\":\"0\",\"IS_VALID\":\"1\",\"UPDATE_FREQ\":\"\",\"TENANT_ID\":\"\",\"CREATION_DATE\":\"\",\"CREATED_BY\":\"\",\"LAST_UPDATE_DATE\":\"\",\"LAST_UPDATED_BY\":\"\",\"LAST_UPDATE_LOGIN\":\"\",\"DELETE_FLAG\":\"0\",\"VERSION_NUM\":\"0\"}]";
List<HashMap<String, Object>> listMap = JsonUtils.toListMap(jsonStr);
JSONArray result = getTreeToParentChildren(listMap, "树", "LABEL_OBJECT,LABEL_TYPE1,LABEL_TYPE2,LABEL_TYPE3,LABEL_TYPE4,LABEL_NAME");
System.out.println("result"+JSON.toJSONString(result));*/
}
}
这篇关于java树形结构的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!