mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 13:34:09 +08:00
Pre Merge pull request !1351 from bioxygenrich25/v5-dev
This commit is contained in:
commit
bef655763f
@ -1,5 +1,6 @@
|
||||
package cn.hutool.core.lang.tree;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.IterUtil;
|
||||
import cn.hutool.core.lang.tree.parser.DefaultNodeParser;
|
||||
import cn.hutool.core.lang.tree.parser.NodeParser;
|
||||
@ -329,4 +330,62 @@ public class TreeUtil {
|
||||
});
|
||||
setChildFunc.accept(root, children);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 将拥有子父级字段的横向列表转成子父级结构的树形列表
|
||||
*
|
||||
* <p>例:</p>
|
||||
* <ul>
|
||||
* <li>totalList:
|
||||
* [
|
||||
* {"id":1,"parentId":0,"name":"level1"},
|
||||
* {"id":2,"parentId":1,"name":"level2"},
|
||||
* {"id":3,"parentId":2,"name":"level3"},
|
||||
* {"id":22,"parentId":1,"name":"level2"},
|
||||
* {"id":5,"parentId":2,"name":"level3"} ]
|
||||
* </li>
|
||||
* <li>rootList:
|
||||
* [{"id":1,"parentId":0,"name":"level1"}]
|
||||
* </li>
|
||||
* <li>parentKey: parentId</li>
|
||||
* <li>slfKey: id</li>
|
||||
* <li>childListFieldName: subList</li>
|
||||
* <li>返回结果:[{"id":1,"parentId":0,"name":"level1","subList":[{"id":2,"parentId":1,"name":"level2","subList":[{"id":3,"parentId":2,"name":"level3"}]},{"id":22,"parentId":1,"name":"level2","subList":[{"id":5,"parentId":2,"name":"level3"}]}]}]</li>
|
||||
* </ul>
|
||||
* @param totalList 总列表(需要树形化的所有数据组成的列表)
|
||||
* @param rootList 根列表(顶级无父级,只包含最顶级的节点列表)
|
||||
* @param parentKey 父级关联的字段名(列表元素中都需要有该字段eg: parentId/parentCode/parentKey)
|
||||
* @param slfKey 列表对象中具备唯一性的字段名(eg:pkId,id,uniqueKey,code......etc.)
|
||||
* @param childListFieldName 子列表的字段名(子列表应为clazz对象的成员变量eg: childList/child/children/subList......etc.)
|
||||
* @throws RuntimeException 入参的list元素对象的属性字段名不一致或者parentKey与slfKey对应的字段为空
|
||||
* @return 树形后的列表
|
||||
* @author bioxygenrich25
|
||||
*/
|
||||
public static List<Map<String, Object>> toTreeView(List<?> totalList, List<?> rootList, String parentKey, String slfKey, String childListFieldName) {
|
||||
List<Map<String, Object>> treeNodes = new ArrayList<>();
|
||||
try {
|
||||
for (Object treeRootNode : rootList) {
|
||||
treeRootNode = buildChildTree(BeanUtil.beanToMap(treeRootNode), totalList, parentKey, slfKey, childListFieldName);
|
||||
treeNodes.add((Map) treeRootNode);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("check whether the list fields are consistent with each other.");
|
||||
}
|
||||
return treeNodes;
|
||||
}
|
||||
|
||||
private static Map buildChildTree(Map pNode, List<?> nodeList, String parentKey, String slfKey, String childKey) {
|
||||
Map<String, Object> pMap = BeanUtil.beanToMap(pNode);
|
||||
List<Map> childTree = new ArrayList<>();
|
||||
for (Object treeNode : nodeList) {
|
||||
Map<String, Object> tMap = BeanUtil.beanToMap(treeNode);
|
||||
if (tMap.get(parentKey).equals(pMap.get(slfKey))) {
|
||||
childTree.add(buildChildTree(BeanUtil.beanToMap(treeNode), nodeList, parentKey, slfKey, childKey));
|
||||
}
|
||||
}
|
||||
pMap.put(childKey, childTree);
|
||||
return pMap;
|
||||
}
|
||||
}
|
||||
|
@ -541,4 +541,17 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除字符串中所有的括号
|
||||
* @param str 原始字符串
|
||||
* @return 返回移除后的字符串
|
||||
* @author bioxygenrich25
|
||||
*/
|
||||
public static String removeAllBrackets(String str) {
|
||||
if (isBlank(str)) {
|
||||
return str;
|
||||
}
|
||||
return str.replaceAll("[()\\[\\]{}<>()【】《》]", "");
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -15,6 +16,17 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
*/
|
||||
public class StrUtilTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void removeAllBrackets() {
|
||||
String r = "[111]";
|
||||
Console.log(StrUtil.removeAllBrackets(r));
|
||||
r = "{222}";
|
||||
Console.log(StrUtil.removeAllBrackets(r));
|
||||
r = "(圆|)[方|]{花|}(中圆|)《书|》<尖|>【000|】";
|
||||
Console.log(StrUtil.removeAllBrackets(r));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isBlankTest() {
|
||||
final String blank = " ";
|
||||
|
@ -0,0 +1,36 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.lang.tree.TreeNode;
|
||||
import cn.hutool.core.lang.tree.TreeUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* @since 2025/5/28 11:09
|
||||
*/
|
||||
public class TreeUtilTest {
|
||||
|
||||
@Test
|
||||
public void toTreeViewTest() {
|
||||
List<TreeNode> allList = new ArrayList<>();
|
||||
allList.add(new TreeNode(10, 0, "level1", null));
|
||||
allList.add(new TreeNode(11, 0, "level1", null));
|
||||
allList.add(new TreeNode(12, 10, "level2", null));
|
||||
allList.add(new TreeNode(13, 10, "level2", null));
|
||||
allList.add(new TreeNode(14, 13, "level3", null));
|
||||
|
||||
List<TreeNode> rootList = new ArrayList<>();
|
||||
rootList.add(new TreeNode(10, 0, "level1", null));
|
||||
rootList.add(new TreeNode(11, 0, "level1", null));
|
||||
|
||||
List<Map<String, Object>> treeList = TreeUtil.toTreeView(allList, rootList, "parentId", "id", "children");
|
||||
Console.log(treeList);
|
||||
Console.log(treeList.size());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user