private void mergeNodes(VrmAssetGraphicalTreeDto existingNode, VrmAssetGraphicalTreeDto newNode) { List existingChildren = existingNode.getChildren(); List newChildren = newNode.getChildren(); // 处理删除节点 existingChildren.removeIf(child -> !hasMatchingChild(newChildren, child)); existingNode.setChildren(existingChildren); // 处理新增/更新节点 for (VrmAssetGraphicalTreeDto newChild : newChildren) { updateOrAddChild(existingNode, newChild); } } private void updateOrAddChild(VrmAssetGraphicalTreeDto existingNode, VrmAssetGraphicalTreeDto newNode) { VrmAssetGraphicalTreeDto matchingChild = findMatchingChild(existingNode.getChildren(), newNode); if (matchingChild != null) { // 有匹配节点,更新节点数据 updateNode(matchingChild, newNode); } else { // 无匹配节点,添加新节点 existingNode.addChild(newNode); } } private void updateNode(VrmAssetGraphicalTreeDto existingNode, VrmAssetGraphicalTreeDto newNode) { // 更新节点属性 existingNode.setName(newNode.getName()); // 处理新增/更新子节点 List existingChildren = existingNode.getChildren(); List newChildren = newNode.getChildren(); for (VrmAssetGraphicalTreeDto newChild : newChildren) { VrmAssetGraphicalTreeDto matchingChild = findMatchingChild(existingChildren, newChild); if (matchingChild != null) { // 有匹配节点,更新节点数据 updateNode(matchingChild, newChild); } else { // 无匹配节点,添加新节点 existingNode.addChild(newChild); } } } private boolean hasMatchingChild(List children, VrmAssetGraphicalTreeDto child) { // 根据id和reqAssetId匹配 return findMatchingChild(children, child) != null; } /** * 找相关匹配 * * @param children * @param childToFind * @return */ private VrmAssetGraphicalTreeDto findMatchingChild(List children, VrmAssetGraphicalTreeDto childToFind) { for (VrmAssetGraphicalTreeDto child : children) { if (child.getId().equals(childToFind.getId()) && child.getReqAssetId().equals(childToFind.getReqAssetId())) { // 已存在的节点不覆盖 return child; } // 递归查找匹配的子节点 VrmAssetGraphicalTreeDto matchingChild = findMatchingChild(child.getChildren(), childToFind); if (matchingChild != null) { return matchingChild; } } return null; } VrmAssetGraphicalTreeDto(id=7104508457453093117, name=技术支撑层, reqAssetId=7104508457453093116, iid=7104508457453093114, reqAssetType=365, relation=false, children=[VrmAssetGraphicalTreeDto(id=7104508457453093157, name=研发管理, reqAssetId=7104508457453093156, iid=7104508457453093154, reqAssetType=366, relation=false, children=[VrmAssetGraphicalTreeDto(id=7104508457453093237, name=BAMS, reqAssetId=7104508457453093236, iid=7104508457453093232, reqAssetType=367, relation=false, children=[VrmAssetGraphicalTreeDto(id=7104508457453093254, name=需求结构化工具, reqAssetId=7104508457453093253, iid=7104508457453093249, reqAssetType=81, relation=false, children=[VrmAssetGraphicalTreeDto(id=7104508457453093285, name=系统管理, reqAssetId=7104508457453093284, iid=7104508457453093280, reqAssetType=387, relation=false, children=[VrmAssetGraphicalTreeDto(id=7104508457453093310, name=用户管理, reqAssetId=7104508457453093309, iid=7104508457453093305, reqAssetType=525, relation=false, children=[VrmAssetGraphicalTreeDto(id=7110683507352928511, name=用户接口, reqAssetId=7110683507352928510, iid=7110683507352928508, reqAssetType=388, relation=true, children=[]), VrmAssetGraphicalTreeDto(id=7110683507352928517, name=外部接口, reqAssetId=7110683507352928516, iid=7110683507352928514, reqAssetType=388, relation=true, children=[])])])])])])])这个数据为existingNode节点的，VrmAssetGraphicalTreeDto(id=7104508457453093117, name=技术支撑层, reqAssetId=7104508457453093116, iid=7104508457453093114, reqAssetType=365, relation=false, children=[VrmAssetGraphicalTreeDto(id=7104508457453093157, name=研发管理, reqAssetId=7104508457453093156, iid=7104508457453093154, reqAssetType=366, relation=false, children=[VrmAssetGraphicalTreeDto(id=7104508457453093237, name=BAMS, reqAssetId=7104508457453093236, iid=7104508457453093232, reqAssetType=367, relation=false, children=[VrmAssetGraphicalTreeDto(id=7104508457453093254, name=需求结构化工具, reqAssetId=7104508457453093253, iid=7104508457453093249, reqAssetType=81, relation=false, children=[VrmAssetGraphicalTreeDto(id=7104508457453093285, name=系统管理, reqAssetId=7104508457453093284, iid=7104508457453093280, reqAssetType=387, relation=false, children=[VrmAssetGraphicalTreeDto(id=7104508457453093310, name=用户管理, reqAssetId=7104508457453093309, iid=7104508457453093305, reqAssetType=525, relation=true, children=[])])])])])])这个数据为newNode节点的，前提，不改变现有逻辑的前提下，合并节点的时候 需要注意newNode节点的relation属性为true合并的时候需要保留，优化一下代码，提供完整示例，中文回答