public class DeleteNode { public static void main(String[] args) { Node node = new Node(1, 1, "aa"); Node node1 = new Node(2, 3, "bb"); Node node2 = new Node(3, 2, "cc"); Node node3 = new Node(4, 3, "dd"); Node node4 = new Node(5, 2, "ee"); List<Node> list = new ArrayList<>(Arrays.asList(node, node1, node2, node3, node4)); List<Node> delete = delete(2, list); delete.forEach(item -> System.out.println(item.toString())); } //第一种 private static List<Node> delete(int targetId, List<Node> nodeList) { List<Node> list = new ArrayList<>(); Stream<Node> nodeStream = nodeList.stream().filter(node -> { return (targetId == node.getId() || targetId == node.getParentId()); }); nodeList.removeAll(nodeStream.collect(Collectors.toList())); list.stream().forEach(node -> { delete(node.getId(), nodeList); }); return nodeList; } //第二种 private static List<Node> delete1(int targetId, List<Node> nodeList) { List<Node> list = new ArrayList<>(); nodeList.stream().forEach(node -> { if (targetId == node.getId() || targetId == node.getParentId()) { list.add(node); } }); nodeList.removeAll(list); list.stream().forEach(node -> { delete(node.getId(), nodeList); }); return nodeList; } }
需要改进,有好的建议欢迎提示
标签:node,...,存储,nodeList,list,List,Node,表中,new From: https://www.cnblogs.com/1024llh/p/17358702.html