A design of a table data extracting process to extract table B

The original title as follows:

Problem-solving code is as follows:

table1 categories:

1 @Data
2 @NoArgsConstructor
3 @AllArgsConstructor
4 public class table1{
5     private String num;
6     private String name;
7     private String fatherNum;
8 }

table2 categories:

 1 @Data
 2 @NoArgsConstructor
 3 @AllArgsConstructor
 4 public class table2{
 5     private String num;
 6     private String name;
 7     private String sheng;
 8     private String shi;
 9     private String qu;
10 }

changeTable categories:

. 1  @Data
 2  @NoArgsConstructor
 . 3  @AllArgsConstructor
 . 4  class the Node {
 . 5      // No 
. 6      Private String NUM;
 . 7      // name 
. 8      Private String name;
 . 9      // parent node 
10      Private the Node fatherNum;
 . 11      // child node of the tree 
12 is      Private List <the node> sonNum;
 13 is  }
 14  @Data
 15  public  class changeTable {
 16      // fragmented node, key number of 
17     Private the Map <String, the Node> = MapData new new the HashMap <> ();
 18 is      // structure with the HashMap, each node in the list are stored in the shape of a tree, where the root node is the province 
. 19      Private List <the Node> = treeData new new the ArrayList <> ();
 20 is      // after processing to generate a new table 
21 is      Private List <Table2> Tab = new new the ArrayList <> ();
 22 is      Private  void createTree (List <table1> DATAS) {
 23 is          for (table1 Data: DATAS) {
 24              // provincial column, added directly to treeData form roots 
25              IF (data.getFatherNum () == null ) {
26 is                  treeData.add (MapData. GET (data.getNum ()));
 27              } the else {
 28                  // current node disposed father point 
29                  MapData. GET (data.getNum ()) setFatherNum (MapData.. GET (Data. getFatherNum ()));
 30                  // parent node is provided by children 
31 is                  List <the node> = sonNum MapData. GET (data.getFatherNum ()) getSonNum ();.
 32                  . sonNum.add (MapData GET (data.getNum ( )));
 33 is                  MapData. GET (data.getFatherNum ()) setSonNum (sonNum);. 
 34 is             }
 35          }
 36      }
 37      // The loose node, to facilitate access 
38 is      Private  void CreateMap (List <table1> DATAS) {
 39          for (table1 Data: DATAS) {
 40              MapData.put (data.getNum (), new new the Node (Data .getNum (), data.getName (), new new the Node (), new new the ArrayList <the Node> ()));
 41 is          }
 42 is      }
 43 is      // generates new table data 
44 is      Private  void createTable2 () {
 45          for (the Node Node: treeData) {
 46 is             // Node is a root, the depth of 0 to 
47              createRow (Node, 0 );
 48          }
 49      }
 50      // use of depth-first traversal generates data 
51 is      Private  void createRow (the Node Node, int depth) {
 52 is          // When reach of layer (layer 0: Province, layer 1: City, layer 2: region, layer 3: company name), then the new data 
53 is          IF (depth == 3 ) {
 54 is              Table2 Table2 = new new Table2 ();
 55              Table2. setNum (node.getNum ());
 56 is              table2.setName (node.getName ());
 57 is             table2.setQu (node.getFatherNum () getNum ().);
 58              table2.setShi (node.getFatherNum () getFatherNum () getNum ()..);
 59              table2.setSheng (node.getFatherNum () getFatherNum ().. getFatherNum () getNum ());.
 60              tab.add (Table2);
 61 is          } the else {
 62 is              // traversing a child node of the current tree node 
63 is              for (the node NOD: node.getSonNum ()) {
 64                  createRow (NOD, depth + . 1 );
 65              }
 66          }
 67      }
 68      public List <Table2> getTable2 (List <table1> dataList) {
69         createMap(dataList);
70         createTree(dataList);
71         createTable2();
72         return tab;
73     }
74 }

Test categories:

. 1  public  class the Test {
 2      public  static  void main (String [] args) {
 . 3          // simulation database query 
. 4          List <table1> = dataList new new the ArrayList <> ();
 . 5          dataList.add ( new new table1 ( " 345 872 234 865 " , " Jianghan District XXX company " , " 342 309 876 534 " ));
 6          dataList.add ( new new table1 ( " 348 724 235 332 " , " Changsha " ,"348653423233"));
 7         dataList.add(new table1("348765064386","武汉市","348653458722"));
 8         dataList.add(new table1("348652342344","岳麓区","348724235332"));
 9         dataList.add(new table1("348653458722",","Hubei Provincenull));
10         dataList.add(new table1("425232324523","岳麓区ZZZ公司","348652342344"));
11         dataList.add(new table1("348653423233","湖南省",null));
12         dataList.add(new table1("342309876534","江汉区","348765064386"));
13 is          dataList.add ( new new table1 ( " 5065438634876 " , " Jianghan YYY company " , " 342309876534 " ));
 14          // Create conversion target 
15          changeTable changeTable = new new changeTable ();
 16          // converts the result 
. 17          List <Table2 > table2s = changeTable.getTable2 (dataList);
 18 is          for (Table2 Tab: table2s) {
 . 19              . the System OUT .println (Tab);
 20 is          }
 21 is      }
 22 is }

Algorithm Analysis:

Comment has been explained in great detail the specific process I will not describe.

Table1 and table2 which are two po object, @data annotated notes equivalent to all the members of the class add the get, set method, @ NoArgsConstructor as a constructor with no arguments, @ AllArgsConstructor for the whole parameter constructor.

operation result:

 

Guess you like

Origin www.cnblogs.com/chengpu/p/algorithm2.html