@Data public class TreeNode { private int id; private int parentId; private String name; private int level; private List<TreeNode> children; public TreeNode(int id, int parentId, String name, int level) { this.id = id; this.parentId = parentId; this.name = name; this.level = level; this.children = new ArrayList<>(); } public Stream<TreeNode> traverseAndSortChildren() { // 如果子节点为空或没有子节点,返回空Stream if (children == null || children.isEmpty()) { return Stream.empty(); } List<String> list2 = Arrays.asList("节点1", "节点2", "节点3", "节点4", "节点9", "节点5", "节点6", "节点7", "节点8", "节点10"); // 创建一个Map,将list2中的每个字符串映射到一个索引,以便我们可以根据这个索引排序list Map<String, Integer> indexMap = IntStream.range(0, list2.size()) .boxed() .collect(Collectors.toMap(list2::get, i -> i)); // 对子节点进行排序(这里以节点名称为例进行排序) List<TreeNode> sortedChildren = children.stream() .sorted(Comparator.comparingInt(obj -> indexMap.getOrDefault(obj.getName(), Integer.MAX_VALUE))) .collect(Collectors.toList()); // 递归遍历并合并子节点的Stream return sortedChildren.stream() .flatMap(child -> Stream.concat( Stream.of(child), // 当前子节点的Stream child.traverseAndSortChildren() // 递归遍历子节点的子节点 )); } }
public class TreeTraversalDemo { // 假设我们有一个方法用于构建多叉树 public static TreeNode buildMultiForkTree() { // 初始化节点并构建多叉树结构... TreeNode treeNode1=new TreeNode(1,0,"节点1",1); TreeNode treeNode2=new TreeNode(2,0,"节点2",1); TreeNode treeNode3=new TreeNode(3,0,"节点3",1); TreeNode treeNode4=new TreeNode(4,0,"节点4",1); TreeNode treeNode5=new TreeNode(5,0,"节点5",1); TreeNode treeNode6=new TreeNode(6,0,"节点6",1); TreeNode treeNode7=new TreeNode(7,0,"节点7",1); TreeNode treeNode8=new TreeNode(8,0,"节点8",1); TreeNode treeNode9=new TreeNode(9,0,"节点9",1); TreeNode treeNode10=new TreeNode(10,0,"节点10",1); ; TreeNode treeNode=new TreeNode(0,0,"节点0",0); TreeNode treeNode11=new TreeNode(11,1,"节点11",2); TreeNode treeNode12=new TreeNode(12,1,"节点12",2); TreeNode treeNode13=new TreeNode(13,1,"节点13",2); List<TreeNode> treeNodes1= Arrays.asList(treeNode11,treeNode12,treeNode13); TreeNode treeNode21=new TreeNode(21,2,"节点21",2); TreeNode treeNode23=new TreeNode(23,2,"节点22",2); TreeNode treeNode22=new TreeNode(22,2,"节点23",2); TreeNode treeNode31=new TreeNode(31,2,"节点31",2); TreeNode treeNode33=new TreeNode(33,2,"节点32",2); TreeNode treeNode32=new TreeNode(32,2,"节点33",2); TreeNode treeNode41=new TreeNode(21,2,"节点41",2); TreeNode treeNode42=new TreeNode(22,2,"节点43",2); TreeNode treeNode43=new TreeNode(23,2,"节点42",2); TreeNode treeNode51=new TreeNode(21,2,"节点51",2); TreeNode treeNode52=new TreeNode(22,2,"节点53",2); TreeNode treeNode53=new TreeNode(23,2,"节点52",2); TreeNode treeNode61=new TreeNode(21,2,"节点61",2); TreeNode treeNode62=new TreeNode(22,2,"节点63",2); TreeNode treeNode63=new TreeNode(23,2,"节点62",2); TreeNode treeNode71=new TreeNode(21,2,"节点71",2); TreeNode treeNode73=new TreeNode(23,2,"节点72",2); TreeNode treeNode72=new TreeNode(22,2,"节点73",2); TreeNode treeNode81=new TreeNode(21,2,"节点81",2); TreeNode treeNode83=new TreeNode(23,2,"节点82",2); TreeNode treeNode82=new TreeNode(22,2,"节点83",2); TreeNode treeNode91=new TreeNode(11111,2,"节点91",2); TreeNode treeNode92=new TreeNode(11112,2,"节点92",2); TreeNode treeNode93=new TreeNode(11113,2,"节点93",2); TreeNode treeNode101=new TreeNode(101,2,"节点101",2); TreeNode treeNode103=new TreeNode(102,2,"节点102",2); TreeNode treeNode102=new TreeNode(103,2,"节点103",2); List<TreeNode> treeNodes2= Arrays.asList(treeNode21,treeNode22,treeNode23); List<TreeNode> treeNodes3= Arrays.asList(treeNode31,treeNode32,treeNode33); List<TreeNode> treeNodes4= Arrays.asList(treeNode41,treeNode42,treeNode43); List<TreeNode> treeNodes5= Arrays.asList(treeNode51,treeNode52,treeNode53); List<TreeNode> treeNodes6= Arrays.asList(treeNode61,treeNode62,treeNode63); List<TreeNode> treeNodes7= Arrays.asList(treeNode71,treeNode72,treeNode73); List<TreeNode> treeNodes8= Arrays.asList(treeNode81,treeNode82,treeNode83); List<TreeNode> treeNodes9= Arrays.asList(treeNode91,treeNode92,treeNode93); List<TreeNode> treeNodes10= Arrays.asList(treeNode101,treeNode102,treeNode103); treeNode1.setChildren(treeNodes1); treeNode2.setChildren(treeNodes2); treeNode3.setChildren(treeNodes3); treeNode4.setChildren(treeNodes4); treeNode5.setChildren(treeNodes5); treeNode6.setChildren(treeNodes6); treeNode7.setChildren(treeNodes7); treeNode8.setChildren(treeNodes8); treeNode9.setChildren(treeNodes9); treeNode10.setChildren(treeNodes10); List<TreeNode> treeNodes= Arrays.asList( treeNode1, treeNode2, treeNode3, treeNode4, treeNode5, treeNode6, treeNode7, treeNode8, treeNode9, treeNode10 ); treeNode.setChildren(treeNodes); // 示例代码省略,你需要根据实际情况构建多叉树 return treeNode; // 返回树的根节点 } // 递归遍历子节点并排序的Stream public static void main(String[] args) { // 构建多叉树(省略具体实现) TreeNode root = buildMultiForkTree(); // 遍历并打印排序后的所有节点(包括子节点和孙子节点等) root.traverseAndSortChildren().forEach(node -> { System.out.println(node.getName()); // 打印节点名称或进行其他操作 }); //一恶搞 List<SortObjectsByTwoLists.MyObject> objects = Arrays.asList( new SortObjectsByTwoLists.MyObject("1", "a"), new SortObjectsByTwoLists.MyObject("2", "a"), new SortObjectsByTwoLists.MyObject("3", "a"), new SortObjectsByTwoLists.MyObject("4", "a"), new SortObjectsByTwoLists.MyObject("2", "b"), new SortObjectsByTwoLists.MyObject("2", "c"), new SortObjectsByTwoLists.MyObject("2", "d"), new SortObjectsByTwoLists.MyObject("3", "b"), new SortObjectsByTwoLists.MyObject("3", "c"), new SortObjectsByTwoLists.MyObject("3", "d") // ... 其他对象 ); List<String> list1 = Arrays.asList("1", "2", "3", "4"); List<String> list2 = Arrays.asList("a", "b", "c", "d"); // 创建两个映射,将list1和list2的元素映射到它们的索引 Map<String, Integer> indexMap1 = IntStream.range(0, list1.size()) .boxed() .collect(Collectors.toMap(list1::get, i -> i)); Map<String, Integer> indexMap2 = IntStream.range(0, list2.size()) .boxed() .collect(Collectors.toMap(list2::get, i -> i * list1.size())); // 乘以list1的大小以区分来自list2的索引 // 使用Stream API对对象列表进行排序 List<SortObjectsByTwoLists.MyObject> sortedObjects = objects.stream() .sorted(Comparator.comparingInt(o -> indexMap2.getOrDefault(o.getProperty1(), Integer.MAX_VALUE) + indexMap1 .getOrDefault(o.getProperty2(), Integer.MAX_VALUE))) .collect(Collectors.toList()); // 输出排序后的结果 sortedObjects.forEach(System.out::println); } }
public class SortObjectsByTwoLists { static class MyObject { private String property1; private String property2; public MyObject(String property1, String property2) { this.property1 = property1; this.property2 = property2; } public String getProperty1() { return property1; } public String getProperty2() { return property2; } @Override public String toString() { return property1 + property2; } } }