zoukankan      html  css  js  c++  java
  • java 解决树形结构数据 (有序无序通杀)

      1 package com.study;
      2 
      3 import java.util.ArrayList;
      4 import java.util.HashMap;
      5 import java.util.List;
      6 import java.util.Map;
      7 import java.util.Map.Entry;
      8 
      9 import com.alibaba.fastjson.JSONArray;
     10 import com.alibaba.fastjson.JSONObject;
     11 
     12 public class SteelTree {
     13 
     14     public static void main(String[] args) {
     15         // get datas
     16         List<Map<String, Object>> records = getRecords();
     17 
     18         // Result JSON
     19         JSONObject result = new JSONObject();
     20 
     21         // record temp json
     22         JSONObject recordJson = new JSONObject();
     23 
     24         // Unknown json
     25         JSONObject unknownRecord = new JSONObject();
     26 
     27         // root id
     28         String firstParent = "00000";
     29 
     30         for (int i = 0, l = records.size(); i < l; i++) {
     31             Map<String, Object> record = records.get(i);
     32 
     33             // if parentid is null.
     34             String parentId;
     35             if (record.get("parentid") == null) {
     36                 // TODO
     37                 parentId = "00000";
     38             } else {
     39                 parentId = record.get("parentid").toString();
     40             }
     41 
     42             // set default JSON and childern node
     43             JSONObject node = new JSONObject();
     44             node.put("id", record.get("id").toString());
     45             node.put("name", record.get("name").toString());
     46             node.put("parentid", parentId);
     47             node.put("children", new JSONArray());
     48 
     49             // if root node
     50             if (parentId.equals(firstParent)) {
     51                 result.put(record.get("id").toString(), node);
     52                 recordJson.put(record.get("id").toString(), node);
     53 
     54                 // if parent exist
     55             } else if (recordJson.containsKey(parentId)) {
     56                 // add children
     57                 recordJson.getJSONObject(parentId).getJSONArray("children").add(node);
     58                 recordJson.put(record.get("id").toString(), node);
     59                 // Unknown relation node
     60             } else {
     61                 String nodeId = record.get("id").toString();
     62                 if (unknownRecord.containsKey(parentId)) {
     63                     // add children
     64                     unknownRecord.getJSONObject(parentId).getJSONArray("children").add(node);
     65                     recordJson.put(nodeId, node);
     66                 } else {
     67                     // find subnode
     68                     for (Entry<String, Object> entry : unknownRecord.entrySet()) {
     69                         JSONObject tempNode = (JSONObject) entry.getValue();
     70                         if (tempNode.getString("parentid").equals(nodeId)) {
     71                             node.getJSONArray("children").add(tempNode);
     72                             recordJson.put(tempNode.get("id").toString(), tempNode);
     73                             unknownRecord.remove(tempNode.get("id").toString());
     74                             break;
     75                         }
     76                     }
     77                     unknownRecord.put(nodeId, node);
     78                 }
     79             }
     80         }
     81 
     82         // unknownRecord add to result
     83         // find subnode
     84         for (Entry<String, Object> entry : unknownRecord.entrySet()) {
     85             JSONObject tempNode = (JSONObject) entry.getValue();
     86             String tempNodeId = tempNode.getString("parentid");
     87             if (recordJson.containsKey(tempNodeId)) {
     88                 // add children
     89                 recordJson.getJSONObject(tempNodeId).getJSONArray("children").add(tempNode);
     90             } else {
     91                 // Error node
     92                 System.out.println("========================ERROR>> " + tempNodeId + "		" + tempNode.toString());
     93             }
     94         }
     95         System.out.println(result.toString());
     96     } // End main
     97 
     98     private static List<Map<String, Object>> getRecords() {
     99         List<Map<String, Object>> result = new ArrayList<>();
    100 
    101         // first record
    102         Map<String, Object> red0101 = new HashMap<>();
    103         red0101.put("id", "U0003");
    104         red0101.put("name", "wangwu");
    105         red0101.put("parentid", "U0001");
    106         result.add(red0101);
    107 
    108         Map<String, Object> red010102 = new HashMap<>();
    109         red010102.put("id", "U0006");
    110         red010102.put("name", "chenba");
    111         red010102.put("parentid", "U0003");
    112         result.add(red010102);
    113 
    114         Map<String, Object> red01 = new HashMap<>();
    115         red01.put("id", "U0001");
    116         red01.put("name", "zhengsan");
    117         red01.put("parentid", "00000");
    118         result.add(red01);
    119 
    120         Map<String, Object> red02 = new HashMap<>();
    121         red02.put("id", "U0002");
    122         red02.put("name", "lisi");
    123         red02.put("parentid", "00000");
    124         result.add(red02);
    125 
    126         Map<String, Object> red0102 = new HashMap<>();
    127         red0102.put("id", "U0004");
    128         red0102.put("name", "zhaolu");
    129         red0102.put("parentid", "U0001");
    130         result.add(red0102);
    131 
    132         Map<String, Object> red010101 = new HashMap<>();
    133         red010101.put("id", "U0005");
    134         red010101.put("name", "maqi");
    135         red010101.put("parentid", "U0003");
    136         result.add(red010101);
    137 
    138         return result;
    139     }
    140 
    141 }
  • 相关阅读:
    August 4th, 2016, Week 32nd, Thursday
    August 3rd, 2016, Week 32nd, Wednesday
    Java的垃圾回收机制
    学java入门到精通,不得不看的15本书
    java中set和get方法的理解
    eclipse快捷键
    main方法无法编译
    Java构造器和方法的区别
    交换两个变量的值,不使用第三个变量
    计算圆周率 Pi (π)值, 精确到小数点后 10000 位 只需要 30 多句代码
  • 原文地址:https://www.cnblogs.com/xikui/p/11383861.html
Copyright © 2011-2022 走看看