package cn.ksource.core.util;
|
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.Comparator;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
|
/**
|
* 多叉树类
|
*/
|
@SuppressWarnings({"rawtypes","unchecked"})
|
public class TreeUtil {
|
public Map createTree(List dataList){
|
// 节点列表(散列表,用于临时存储节点对象)
|
HashMap nodeList = new HashMap();
|
// 根节点
|
Node root = new Node();
|
root.text = "根节点";
|
root.id = "1";
|
root.parentId = "0";
|
root.index = "1";
|
// 根据结果集构造节点列表(存入散列表)
|
for (Iterator it = dataList.iterator(); it.hasNext();) {
|
Map dataRecord = (Map) it.next();
|
Node node = new Node();
|
node.id = (String) dataRecord.get("id");
|
node.text = (String) dataRecord.get("text");
|
node.parentId = (String) dataRecord.get("parentId");
|
node.index = dataRecord.get("sortId").toString();
|
nodeList.put(node.id, node);
|
}
|
// 构造无序的多叉树
|
Set entrySet = nodeList.entrySet();
|
for (Iterator it = entrySet.iterator(); it.hasNext();) {
|
Node node = (Node) ((Map.Entry) it.next()).getValue();
|
if (node.parentId == null || node.parentId.equals("")) {
|
root.addChild(node);
|
} else {
|
((Node) nodeList.get(node.parentId)).addChild(node);
|
}
|
}
|
// 输出无序的树形菜单的JSON字符串
|
//System.out.println(root.toString());
|
// 对多叉树进行横向排序
|
root.sortChildren();
|
// 输出有序的树形菜单的JSON字符串
|
//System.out.println(root.toString());
|
Map map = JsonUtil.json2Map(root.toString());
|
return map;
|
}
|
|
public static Map createTreeByListMap(List<Map> dataList){
|
Map root = new HashMap();
|
root.put("text", "根节点");
|
List list = new ArrayList<Map>();
|
root.put("children", list);
|
//将数据LIST放置到MAP中
|
Map<String,Object> dataMap = new LinkedHashMap();
|
if(dataList!=null&&dataList.size()>0){
|
for(Map map:dataList){
|
dataMap.put(map.get("id").toString(),map);
|
}
|
}
|
//组装树
|
for(String key:dataMap.keySet()){
|
if(((Map)dataMap.get(key)).get("pid")==null||((Map)dataMap.get(key)).get("pid").toString().equals("")||((Map)dataMap.get(key)).get("pid").toString().equals("0")){
|
((List<Map>)root.get("children")).add((Map)dataMap.get(key));
|
}else{
|
Map parentNode = (Map)dataMap.get(((Map)dataMap.get(key)).get("pid").toString());
|
if(parentNode.get("children")==null){
|
List children = new ArrayList<Map>();
|
parentNode.put("children", children);
|
}
|
((List<Map>)parentNode.get("children")).add((Map)dataMap.get(key));
|
}
|
}
|
//System.out.println("----------------------"+JsonUtil.map2Json(root));
|
return root;
|
}
|
|
public static List<Map> createTreeByList(List<Map> dataList){
|
Map root = new HashMap();
|
root.put("text", "根节点");
|
List list = new ArrayList<Map>();
|
root.put("children", list);
|
if(dataList!=null&&dataList.size()>0){
|
//将数据LIST放置到MAP中
|
Map<String,Object> dataMap = new LinkedHashMap();
|
if(dataList!=null&&dataList.size()>0){
|
for(Map map:dataList){
|
dataMap.put(map.get("id").toString(),map);
|
}
|
}
|
//组装树
|
for(String key:dataMap.keySet()){
|
if(((Map)dataMap.get(key)).get("pid")==null||((Map)dataMap.get(key)).get("pid").toString().equals("")||((Map)dataMap.get(key)).get("pid").toString().equals("0")){
|
((List<Map>)root.get("children")).add((Map)dataMap.get(key));
|
}else{
|
Map parentNode = (Map)dataMap.get(((Map)dataMap.get(key)).get("pid").toString());
|
if(parentNode!=null){
|
if(parentNode.get("children")==null){
|
List children = new ArrayList<Map>();
|
parentNode.put("children", children);
|
}
|
((List<Map>)parentNode.get("children")).add((Map)dataMap.get(key));
|
}
|
}
|
}
|
}
|
if(root.get("children")!=null){
|
return (List<Map>)root.get("children");
|
}else{
|
return null;
|
}
|
}
|
|
public static Map createTreeByListHasChildren(List<Map> dataList){
|
Map root = new HashMap();
|
root.put("text", "根节点");
|
List list = new ArrayList<Map>();
|
root.put("children", list);
|
Map<String,Object> dataMap = new LinkedHashMap();
|
if(dataList!=null&&dataList.size()>0){
|
for(Map map:dataList){
|
List children = new ArrayList<Map>();
|
map.put("children", children);
|
dataMap.put(map.get("id").toString(),map);
|
}
|
}
|
//组装树
|
for(String key:dataMap.keySet()){
|
if(((Map)dataMap.get(key)).get("pid")==null||((Map)dataMap.get(key)).get("pid").toString().equals("")||((Map)dataMap.get(key)).get("pid").toString().equals("0")){
|
((List<Map>)root.get("children")).add((Map)dataMap.get(key));
|
}else{
|
Map parentNode = (Map)dataMap.get(((Map)dataMap.get(key)).get("pid").toString());
|
((List<Map>)parentNode.get("children")).add((Map)dataMap.get(key));
|
}
|
}
|
//System.out.println("----------------------"+JsonUtil.map2Json(root));
|
return root;
|
}
|
|
|
public Map createTree(List<Map> categoryList,List<Map> queryList){
|
Map root = new HashMap();
|
root.put("text", "根节点");
|
List list = new ArrayList<Map>();
|
root.put("children", list);
|
//将数据LIST放置到MAP中
|
Map<String,Object> queryMap = new HashMap();
|
if(queryList!=null&&queryList.size()>0){
|
for(Map map:queryList){
|
queryMap.put(map.get("id").toString(),map);
|
}
|
}
|
//将类型LIST放置到MAP中
|
Map<String,Object> categoryMap = new HashMap();
|
for(Map map:categoryList){
|
categoryMap.put(map.get("id").toString(), map);
|
}
|
|
//组装树
|
for(String key:categoryMap.keySet()) {
|
Map node = (Map)categoryMap.get(key);
|
if(queryMap.get(key)!=null){
|
node.put("dataMap", queryMap.get(key));
|
}
|
if(node.get("parentId")==null||node.get("parentId").toString().equals("")||node.get("parentId").toString().equals("0")){
|
((List<Map>)root.get("children")).add(node);
|
}else{
|
Map parentNode = ((Map)categoryMap.get(node.get("parentId").toString()));
|
if(parentNode.get("children")==null){
|
List children = new ArrayList<Map>();
|
parentNode.put("children", children);
|
}
|
((List<Map>)parentNode.get("children")).add(node);
|
}
|
}
|
//System.out.println("----------------------"+JsonUtil.map2Json(root));
|
return root;
|
}
|
|
|
public Map createTree(List<Map> categoryList,List<Map> queryList,List<Map> lebalList){
|
Map root = new HashMap();
|
root.put("text", "根节点");
|
List list = new ArrayList<Map>();
|
root.put("children", list);
|
|
Map<String,Object> queryMap = new HashMap();
|
if(queryList!=null&&queryList.size()>0){
|
for(Map map:queryList){
|
queryMap.put(map.get("id").toString(),map);
|
}
|
}
|
|
Map<String,Object> categoryMap = new HashMap();
|
for(Map map:categoryList){
|
categoryMap.put(map.get("id").toString(), map);
|
}
|
|
for(String key:categoryMap.keySet()) {
|
Map node = (Map)categoryMap.get(key);
|
List<String> tempList = new ArrayList<String>();
|
for(Map map:lebalList){
|
String name = key+"_"+map.get("id").toString();
|
if(queryMap.get(name)!=null){
|
tempList.add(((Map)queryMap.get(name)).get("num").toString());
|
}else{
|
tempList.add("-");
|
}
|
}
|
node.put("dataList", tempList);
|
|
|
if(node.get("parentId")==null||node.get("parentId").toString().equals("")||node.get("parentId").toString().equals("0")){
|
((List<Map>)root.get("children")).add(node);
|
}else{
|
Map parentNode = ((Map)categoryMap.get(node.get("parentId").toString()));
|
if(parentNode!=null){
|
if(parentNode.get("children")==null){
|
List children = new ArrayList<Map>();
|
parentNode.put("children", children);
|
}
|
((List<Map>)parentNode.get("children")).add(node);
|
}
|
|
|
}
|
}
|
System.out.println("----------------------"+JsonUtil.map2Json(root));
|
return root;
|
}
|
|
|
public List<Map> createTreeByCate(List<Map> categoryList,List<Map> dataList){
|
//将类型LIST放置到MAP中
|
Map<String,Object> categoryMap = new HashMap();
|
for(Map map:categoryList){
|
List children = new ArrayList<Map>();
|
map.put("children", children);
|
categoryMap.put(map.get("id").toString(), map);
|
}
|
|
for(Map data:dataList){
|
if(categoryMap.get(data.get("cateId").toString())!=null){
|
((List<Map>)((Map)categoryMap.get(data.get("cateId").toString())).get("children")).add(data);
|
}
|
}
|
return categoryList;
|
}
|
public List<Map> createTreeByLvlId(List<Map> categoryList,List<Map> dataList){
|
//将类型LIST放置到MAP中
|
Map<String,Object> categoryMap = new HashMap();
|
for(Map map:categoryList){
|
List children = new ArrayList<Map>();
|
map.put("children", children);
|
categoryMap.put(map.get("ID").toString(), map);
|
}
|
|
for(Map data:dataList){
|
if(categoryMap.get(data.get("ID").toString())!=null){
|
((List<Map>)((Map)categoryMap.get(data.get("ID").toString())).get("children")).add(data);
|
}
|
}
|
return categoryList;
|
}
|
}
|
|
class Node {
|
/**
|
* 节点编号
|
*/
|
public String id;
|
/**
|
* 节点内容
|
*/
|
public String text;
|
/**
|
* 父节点编号
|
*/
|
public String parentId;
|
/**
|
* 孩子节点列表
|
*/
|
public String index;
|
|
public Map dataMap;
|
|
public Integer size;
|
|
private Children children = new Children();
|
|
// 先序遍历,拼接JSON字符串
|
public String toString() {
|
String result = "{" + "id : '" + id + "'" + ", text : '" + text + "'";
|
|
if (children != null && children.getSize() != 0) {
|
result += ", children : " + children.toString();
|
} else {
|
result += ", leaf : true";
|
}
|
|
return result + "}";
|
}
|
|
// 兄弟节点横向排序
|
public void sortChildren() {
|
if (children != null && children.getSize() != 0) {
|
children.sortChildren();
|
}
|
}
|
|
// 添加孩子节点
|
public void addChild(Node node) {
|
this.children.addChild(node);
|
}
|
|
|
/**
|
* 孩子列表类
|
*/
|
class Children {
|
private List list = new ArrayList();
|
|
public int getSize() {
|
return list.size();
|
}
|
|
public void addChild(Node node) {
|
list.add(node);
|
}
|
|
// 拼接孩子节点的JSON字符串
|
public String toString() {
|
String result = "[";
|
for (Iterator it = list.iterator(); it.hasNext();) {
|
result += ((Node) it.next()).toString();
|
result += ",";
|
}
|
result = result.substring(0, result.length() - 1);
|
result += "]";
|
return result;
|
}
|
|
// 孩子节点排序
|
public void sortChildren() {
|
// 对本层节点进行排序
|
// 可根据不同的排序属性,传入不同的比较器,这里传入ID比较器
|
Collections.sort(list, new NodeIDComparator());
|
// 对每个节点的下一层节点进行排序
|
for (Iterator it = list.iterator(); it.hasNext();) {
|
((Node) it.next()).sortChildren();
|
}
|
}
|
}
|
|
/**
|
* 节点比较器
|
*/
|
class NodeIDComparator implements Comparator {
|
// 按照节点编号比较
|
public int compare(Object o1, Object o2) {
|
int j1 = Integer.parseInt(((Node) o1).index);
|
int j2 = Integer.parseInt(((Node) o2).index);
|
return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));
|
}
|
}
|
|
}
|