/**
* 根据类型列表构建树结构
*
* @param rootId
* @param typeList
* @return
*/
public static Map<String, Object> buildTypeTree(String rootId, List<Map<String, Object>> typeList) {
Map<String, Object> treeMap = new HashMap<String, Object>();
treeMap.put("ID", rootId);
for (Map<String, Object> m : typeList) {
String idPath = (String) m.get("IDPATH");
if (idPath == null) {
treeMap.put("ID", m.get("ID"));
treeMap.put("NAME", m.get("NAME"));
continue;
}
String[] ids = idPath.split("->");
m.remove("IDPATH");
if (ids.length > 0)
createTree(treeMap, ids, 0, m);
}
return treeMap;
}
public static void createTree(Map<String, Object> treeMap, String[] ids, int index, Map<String, Object> nodeMap) {
List<Map<String, Object>> childList = (List<Map<String, Object>>) treeMap.get("CHILD");
if (childList == null) {
treeMap.put("CHILD", new ArrayList<Map<String, Object>>());
childList = (List<Map<String, Object>>) treeMap.get("CHILD");
}
if (childList.size() == 0) {
if (index < ids.length) {
if (ids.length - index == 1) {
childList.add(nodeMap);
} else {
Map<String, Object> childMap = new HashMap<String, Object>();
childMap.put("ID", ids[index]);
childList.add(childMap);
createTree(childMap, ids, ++index, nodeMap);
}
}
} else {
boolean add = true;
for (Map<String, Object> childMap : childList) {
if (childMap.get("ID").equals(ids[index])) {
add = false;
if (ids.length - index == 1) {
childMap.putAll(nodeMap);
} else {
createTree(childMap, ids, ++index, nodeMap);
}
}
}
if (add) {
if (ids.length - index == 1) {
childList.add(nodeMap);
} else {
Map<String, Object> childMap = new HashMap<String, Object>();
childMap.put("ID", ids[index]);
childList.add(childMap);
createTree(childMap, ids, ++index, nodeMap);
}
}
}
}
/**
* 根据code对事件排序
* @param child
*/
private void sort(List<Map<String, Object>> child) {
Collections.sort(child, new Comparator<Map<String, Object>>() {
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
String code1 = (String) o1.get(("CODE"));
String code2 = (String) o2.get(("CODE"));
if (code1 == null && code2 == null) {
return 0;
} else if (code1 != null && code2 == null) {
return 1;
} else if (code1 == null && code2 != null) {
return -1;
} else if (code1 != null && code2 != null) {
return code1.compareToIgnoreCase(code2);
}
return -1;
}
});
List<Map<String, Object>> $mapList1 = new ArrayList<>();
List<Map<String, Object>> $mapList2 = new ArrayList<>();
for (int i = 0, j = child.size(); i < j; i++) {
String code = (String) child.get(i).get("CODE");
if (code == null) {
$mapList2.add($mapList1.size(), child.get(i));
} else {
$mapList1.add(child.get(i));
}
}
$mapList1.addAll($mapList2);
}
public static void sort(Map<String, Object> treeMap) {
List<Map<String, Object>> child = (List<Map<String, Object>>) treeMap.get("CHILD");
if (child != null) {
sort(child);
for (Map<String, Object> map : child) {
sort(map);
}
}
}
String sql="select substr(sys_connect_by_path(parent_id,'->'),3) idpath,id,name from sc_t_affair_type start with parent_id='00001' connect by prior id=parent_id";
public static void main(String ar[]) throws Exception{
String[] ids1 = {"1","2","3","4"};
String[] ids11 = {"1","2","3","5"};
String[] ids2 = {"1","22"};
String[] ids22 = {"1","222"};
String[][] ids={ids1,ids11,ids2,ids22};
Map<String,Object> treeMap=new HashMap<String,Object>();
treeMap.put("CHILD",new ArrayList<Map<String,Object>>());
treeMap.put("ID","1");
for(int i=0;i<ids.length;i++){
Map<String,String> nodeMap=new HashMap<>();
nodeMap.put("NAME","name"+i);
nodeMap.put("ID", ids[i][ids[i].length - 1]);
createTree(treeMap, ids[i], 1, nodeMap);
}
System.out.println(treeMap);
}
public static void createTree(Map<String,Object> treeMap,String[] ids,int index, Map<String,String> nodeMap){
List<Map<String,Object>> childList=(List<Map<String,Object>>)treeMap.get("CHILD");
if(childList==null){
treeMap.put("CHILD",new ArrayList<Map<String,Object>>());
childList=(List<Map<String,Object>>)treeMap.get("CHILD");
}
if(childList.size()==0){
Map<String,Object> childMap=new HashMap<String,Object>();
childMap.put("ID",ids[index++]);
childList.add(childMap);
System.out.println(ids.length);
if(index<ids.length)
createTree(childMap,ids,index,nodeMap);
}else{
boolean add=true;
for(Map<String,Object> map:childList){
if(ids[index].equals(map.get("ID"))){
add=false;
createTree(map,ids,++index,nodeMap);
}
}
if(add){
Map<String,Object> childMap=new HashMap<String,Object>();
childMap.put("ID",ids[index++]);
childList.add(childMap);
if(index<ids.length)
createTree(childMap,ids,index,nodeMap);
}
}
}