Javaは親ノードに基づいてすべての子ノードを再帰する
15725 ワード
エンティティークラス
データの準備
Javaは親ノードに基づいてすべてのサブノードコアコードを再帰する
テストコード:
結果出力:
ok
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrgExcelCnDTO {
//
@Excel(name = " *", width = 30,orderNum = "0")
private String excelNumberId;
//
@Excel(name = " *", width = 30,orderNum = "1")
private String name;
//
@Excel(name = " * ( )", width = 35,orderNum = "2")
private String parentExcelNumberId;
// (1. 2. )
//@Excel(name = " ( / )", replace = {" _1", " _2"}, width = 40,orderNum = "3")
@Excel(name = " ( / )", width = 40,orderNum = "3")
private String excelType;
private Integer type;
// (1. 2. 3. )
//@Excel(name = " ( / / )", replace = {" _1", " _2", " _3"}, width = 50,orderNum = "4")
@Excel(name = " ( / / )", width = 50,orderNum = "4")
private String excelCategory;
.....
.....
}
データの準備
OrgExcelCnDTO(excelNumberId=1, name= , parentExcelNumberId=null, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=2, name= , parentExcelNumberId=null, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=3, name= 001 3, parentExcelNumberId=2, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=4, name= 001 4, parentExcelNumberId=1, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=5, name= 001 5, parentExcelNumberId=2, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=6, name= 001 6, parentExcelNumberId=1, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=7, name= 001 7, parentExcelNumberId=2, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=8, name= 001 8, parentExcelNumberId=1, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=9, name= 002 9, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=10, name= 002 10, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=11, name= 002 11, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=12, name= 002 12, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=13, name= 002 13, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=14, name= 002 14, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=15, name= 002 15, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=16, name= 002 16, parentExcelNumberId=7, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=17, name= 002 17, parentExcelNumberId=7, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=18, name= 002 18, parentExcelNumberId=7, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=19, name= 002 19, parentExcelNumberId=7, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=20, name= 002 20, parentExcelNumberId=7, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
Javaは親ノードに基づいてすべてのサブノードコアコードを再帰する
//
public static List getChildNodes(List list, String parentId) {
for (OrgExcelCnDTO dto : list) {
// id id,add
if (parentId.equals(dto.getParentExcelNumberId())) {
//
getChildNodes(list,dto.getExcelNumberId());
orgChildNodes.add(dto);
}
}
return orgChildNodes;
}
テストコード:
package com.hierway.ims.employee.util;
import com.hierway.ims.employee.controller.resp.OrgExcelCnDTO;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
/**
* @Description
* @Author by mocar
* @Date 2020/6/12 17:30
**/
public class RecursiveTest {
//
public static List orgChildNodes = new ArrayList();
public static void main(String[] args) {
List data = getData();
List childNodes = getChildNodes(data, "1");
System.out.println();
System.out.println();
System.out.println("-------------- -------------");
childNodes.forEach(vo-> System.out.println(vo));
}
public static List getChildNodes(List list, String parentId) {
for (OrgExcelCnDTO dto : list) {
// id id,add
if (parentId.equals(dto.getParentExcelNumberId())) {
//
getChildNodes(list,dto.getExcelNumberId());
orgChildNodes.add(dto);
}
}
return orgChildNodes;
}
public static List getData(){
List list = new ArrayList<>();
OrgExcelCnDTO orgExcelCnDTO = new OrgExcelCnDTO();
OrgExcelCnDTO orgExcelCnDTO2 = new OrgExcelCnDTO();
orgExcelCnDTO.setExcelNumberId("1");
orgExcelCnDTO.setName(" ");
orgExcelCnDTO2.setExcelNumberId("2");
orgExcelCnDTO2.setName(" ");
list.add(orgExcelCnDTO);
list.add(orgExcelCnDTO2);
for (Integer i = 3; i <= 8; i++) {
OrgExcelCnDTO temOrg = new OrgExcelCnDTO();
temOrg.setExcelNumberId(i.toString());
if (i % 2 == 0) {
temOrg.setName(" 001 " + i);
temOrg.setParentExcelNumberId("1");
} else {
temOrg.setName(" 001 " + i);
temOrg.setParentExcelNumberId("2");
}
list.add(temOrg);
}
for (Integer i = 9; i <= 15; i++) {
OrgExcelCnDTO temppOrg = new OrgExcelCnDTO();
temppOrg.setExcelNumberId(i.toString());
temppOrg.setName(" 002 " + i);
temppOrg.setParentExcelNumberId("6");
list.add(temppOrg);
}
for (Integer i = 16; i <= 20; i++) {
OrgExcelCnDTO temppOrg = new OrgExcelCnDTO();
temppOrg.setExcelNumberId(i.toString());
temppOrg.setName(" 002 " + i);
temppOrg.setParentExcelNumberId("7");
list.add(temppOrg);
}
list.forEach(vo-> System.out.println(vo));
return list;
}
}
結果出力:
OrgExcelCnDTO(excelNumberId=1, name= , parentExcelNumberId=null, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=4, name= 001 4, parentExcelNumberId=1, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=6, name= 001 6, parentExcelNumberId=1, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=8, name= 001 8, parentExcelNumberId=1, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=9, name= 002 9, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=10, name= 002 10, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=11, name= 002 11, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=12, name= 002 12, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=13, name= 002 13, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=14, name= 002 14, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=15, name= 002 15, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
-------------- -------------
OrgExcelCnDTO(excelNumberId=4, name= 001 4, parentExcelNumberId=1, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=9, name= 002 9, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=10, name= 002 10, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=11, name= 002 11, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=12, name= 002 12, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=13, name= 002 13, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=14, name= 002 14, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=15, name= 002 15, parentExcelNumberId=6, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=6, name= 001 6, parentExcelNumberId=1, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
OrgExcelCnDTO(excelNumberId=8, name= 001 8, parentExcelNumberId=1, excelType=null, type=null, excelCategory=null, category=null, note=null, id=null, code=null, parentId=null, createUser=null, createTime=null, modifyUser=null, modifyTime=null, errorMsg=null)
ok