提交 ee3a5be8 作者: obcy

添加树处理工具

上级 d19535e0
package com.zzsn.code.base.core.util.collectionutils;
import java.util.Collection;
/**
* Description:
*
* @author: EDY
* @since: 2023/12/27
*/
public class CollectionUtils {
public static boolean isNotEmpty(Collection<?> coll) {
return !isEmpty(coll);
}
public static boolean isEmpty(Collection<?> coll) {
return coll == null || coll.isEmpty();
}
}
package com.zzsn.code.base.core.util.objutil;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
/**
* Description:
*
* @author: EDY
* @since: 2023/12/27
*/
public class ObjectUtils {
public static boolean isNotEmpty(final Object object) {
return !isEmpty(object);
}
public static boolean isEmpty(final Object object) {
if (object == null) {
return true;
}
if (object instanceof CharSequence) {
return ((CharSequence) object).length() == 0;
}
if (object.getClass().isArray()) {
return Array.getLength(object) == 0;
}
if (object instanceof Collection<?>) {
return ((Collection<?>) object).isEmpty();
}
if (object instanceof Map<?, ?>) {
return ((Map<?, ?>) object).isEmpty();
}
return false;
}
}
package com.zzsn.code.base.core.util.strutil;
/**
* Description:
*
* @author: EDY
* @since: 2023/12/27
*/
public class StringUtils {
/**
* <p>Checks if a CharSequence is not empty ("") and not null.</p>
*
* <pre>
* StringUtils.isNotEmpty(null) = false
* StringUtils.isNotEmpty("") = false
* StringUtils.isNotEmpty(" ") = true
* StringUtils.isNotEmpty("bob") = true
* StringUtils.isNotEmpty(" bob ") = true
* </pre>
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is not empty and not null
* @since 3.0 Changed signature from isNotEmpty(String) to isNotEmpty(CharSequence)
*/
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}
/**
* <p>Checks if a CharSequence is empty ("") or null.</p>
*
* <pre>
* StringUtils.isEmpty(null) = true
* StringUtils.isEmpty("") = true
* StringUtils.isEmpty(" ") = false
* StringUtils.isEmpty("bob") = false
* StringUtils.isEmpty(" bob ") = false
* </pre>
*
* <p>NOTE: This method changed in Lang version 2.0.
* It no longer trims the CharSequence.
* That functionality is available in isBlank().</p>
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is empty or null
* @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
*/
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
/**
* <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
*
* <pre>
* StringUtils.isNotBlank(null) = false
* StringUtils.isNotBlank("") = false
* StringUtils.isNotBlank(" ") = false
* StringUtils.isNotBlank("bob") = true
* StringUtils.isNotBlank(" bob ") = true
* </pre>
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is
* not empty and not null and not whitespace only
* @since 2.0
* @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
*/
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}
/**
* <p>Checks if a CharSequence is empty (""), null or whitespace only.</p>
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
*
* <pre>
* StringUtils.isBlank(null) = true
* StringUtils.isBlank("") = true
* StringUtils.isBlank(" ") = true
* StringUtils.isBlank("bob") = false
* StringUtils.isBlank(" bob ") = false
* </pre>
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is null, empty or whitespace only
* @since 2.0
* @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
*/
public static boolean isBlank(final CharSequence cs) {
final int strLen = length(cs);
if (strLen == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
/**
* Gets a CharSequence length or {@code 0} if the CharSequence is
* {@code null}.
*
* @param cs
* a CharSequence or {@code null}
* @return CharSequence length or {@code 0} if the CharSequence is
* {@code null}.
* @since 2.4
* @since 3.0 Changed signature from length(String) to length(CharSequence)
*/
public static int length(final CharSequence cs) {
return cs == null ? 0 : cs.length();
}
}
package com.zzsn.code.base.core.util.treeutil;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* Entity基类
* @author hydsj
*/
public class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 协会id
*/
private String fkOwnerId;
/**
* 协会名称
*/
private String fkOwnerName;
/**
* 机构ID
*/
private String fkOrgSid;
/**
* 部门ID
*/
private Long fkDeptId;
/**
* 搜索值
*/
private String searchValue;
/**
* 创建者
*/
protected String createBy;
/**
* 创建者名称
*/
protected String createByName;
/**
* 创建时间
*/
protected Date createTime;
/**
* 更新者
*/
protected String updateBy;
/**
* 更新者名称
*/
protected String updateByName;
/**
* 更新时间
*/
protected Date updateTime;
/**
* 备注
*/
private String remark;
/**
* 请求参数
*/
private Map<String, Object> params;
public String getSearchValue() {
return searchValue;
}
public void setSearchValue(String searchValue) {
this.searchValue = searchValue;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getUpdateBy() {
return updateBy;
}
public void setUpdateBy(String updateBy) {
this.updateBy = updateBy;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Map<String, Object> getParams() {
if (params == null) {
params = new HashMap<>();
}
return params;
}
public void setParams(Map<String, Object> params) {
this.params = params;
}
public String getCreateByName() {
return createByName;
}
public void setCreateByName(String createByName) {
this.createByName = createByName;
}
public String getUpdateByName() {
return updateByName;
}
public void setUpdateByName(String updateByName) {
this.updateByName = updateByName;
}
public String getFkOwnerId() {
return fkOwnerId;
}
public void setFkOwnerId(String fkOwnerId) {
this.fkOwnerId = fkOwnerId;
}
public String getFkOwnerName() {
return fkOwnerName;
}
public void setFkOwnerName(String fkOwnerName) {
this.fkOwnerName = fkOwnerName;
}
public String getFkOrgSid() {
return fkOrgSid;
}
public void setFkOrgSid(String fkOrgSid) {
this.fkOrgSid = fkOrgSid;
}
public Long getFkDeptId() {
return fkDeptId;
}
public void setFkDeptId(Long fkDeptId) {
this.fkDeptId = fkDeptId;
}
}
package com.zzsn.code.base.core.util.treeutil;
import java.util.List;
/**
* @author lkg
* @description: 树形节点
* @date 2021/12/15 14:11
*/
public class Node {
public Node() {
}
public Node(String id, String name, String pid, Integer level, Integer orderNo, Integer hasChild, List<? extends Node> children) {
this.id = id;
this.name = name;
this.pid = pid;
this.level = level;
this.orderNo = orderNo;
this.hasChild = hasChild;
this.children = children;
}
private String id;
private String name;
private String pid;
private Integer level;
private Integer orderNo;
private Integer hasChild;
private List<? extends Node> children;
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPid(String pid) {
this.pid = pid;
}
public void setLevel(Integer level) {
this.level = level;
}
public void setOrderNo(Integer orderNo) {
this.orderNo = orderNo;
}
public void setHasChild(Integer hasChild) {
this.hasChild = hasChild;
}
public void setChildren(List<? extends Node> children) {
this.children = children;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getPid() {
return pid;
}
public Integer getLevel() {
return level;
}
public Integer getOrderNo() {
return orderNo;
}
public Integer getHasChild() {
return hasChild;
}
public List<? extends Node> getChildren() {
return children;
}
}
package com.zzsn.code.base.core.util.treeutil;
import java.util.ArrayList;
import java.util.List;
/**
* Tree基类
* @author hydsj
*/
public class TreeEntity extends BaseEntity {
private static final long serialVersionUID = 1L;
private Long id;
/**
* 父菜单名称
*/
private String parentName;
/**
* 父菜单ID
*/
private Long parentId;
/**
* 显示顺序
*/
private Integer orderNum;
/**
* 祖级列表
*/
private String ancestors;
/**
* 状态
*/
private boolean checked = false;
/**
* 子部门
*/
private List children = new ArrayList();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getParentName() {
return parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
public Long getParentId() {
return parentId;
}
public void setParentId(Long parentId) {
this.parentId = parentId;
}
public Integer getOrderNum() {
return orderNum;
}
public void setOrderNum(Integer orderNum) {
this.orderNum = orderNum;
}
public String getAncestors() {
return ancestors;
}
public void setAncestors(String ancestors) {
this.ancestors = ancestors;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
}
package com.zzsn.code.base.core.util.treeutil;
import com.zzsn.code.base.core.util.collectionutils.CollectionUtils;
import com.zzsn.code.base.core.util.objutil.ObjectUtils;
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author lkg
* @description: 构造树形结构工具类
* @date 2021/12/15 14:18
*/
public class TreeUtil {
/**
* 单个树
*
* @param list 数据集合
* @param node 根节点数据
* @author lkg
* @date 2021/12/17 16:33
*/
public static <T extends Node> void tree(List<T> list, T node) {
String id = node.getId();
List<T> tree = tree(list, id);
node.setChildren(tree);
}
/**
* 树 集合
*
* @param list 数据集合
* @param rootId 根节点id
* @author lkg
* @date 2021/12/17 16:33
*/
public static <T extends Node> List<T> tree(List<T> list, String rootId) {
Map<String, List<T>> treeMap = new HashMap<>();
for (T item : list) {
String parentId = item.getPid();
List<T> children;
if (treeMap.containsKey(parentId)) {
children = treeMap.get(parentId);
} else {
children = new ArrayList<>();
treeMap.put(parentId, children);
}
children.add(item);
}
return getTreeByMap(treeMap, rootId);
}
/**
* 根据当前节点获取所有上级节点
*
* @param list 数据集合
* @param currentNode 当前节点
* @param flag 是否包含自己本身(true-是;false-否)
* @author lkg
* @date 2022/12/27 11:32
*/
public static <T extends Node> LinkedHashSet<T> getUpList(List<T> list, T currentNode, boolean flag) {
if (ObjectUtils.isNotEmpty(currentNode)) {
LinkedHashSet<T> treeSet = new LinkedHashSet<>();
if (flag) {
treeSet.add(currentNode);
}
String pid = currentNode.getPid();
List<T> parentNodes = list.stream().filter(node -> node.getId().equals(pid)).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(parentNodes)) {
T parentNode = parentNodes.get(0);
treeSet.add(parentNode);
LinkedHashSet<T> upTreeSet = getUpList(list, parentNode, flag);
if (CollectionUtils.isNotEmpty(upTreeSet)) {
treeSet.addAll(upTreeSet);
}
}
return treeSet;
}
return null;
}
/**
* 获取某节点下所有节点id得集合
*
* @param list: 数据集合
* @param rootId: 节点id
* @param flag: 是否包含自己本身(true-是;false-否)
* @author lkg
* @date 2023/2/23
*/
public static <T extends Node> List<String> belowList(List<T> list, String rootId, boolean flag) {
List<String> nodes = new ArrayList<>();
if (flag) {
nodes.add(rootId);
}
Map<String, List<T>> treeMap = new HashMap<>();
for (T item : list) {
String parentId = item.getPid();
List<T> children;
if (treeMap.containsKey(parentId)) {
children = treeMap.get(parentId);
} else {
children = new ArrayList<>();
treeMap.put(parentId, children);
}
children.add(item);
}
getBelowList(treeMap, rootId, nodes);
return nodes;
}
private static <T extends Node> List<T> getBelowList(Map<String, List<T>> treeMap, String parentId, List<String> belowList) {
List<T> children = treeMap.get(parentId);
if (children == null) {
return null;
}
for (T item : children) {
belowList.add(item.getId());
item.setChildren(getBelowList(treeMap, item.getId(), belowList));
}
return children;
}
private static <T extends Node> List<T> getTreeByMap(Map<String, List<T>> treeMap, String parentId) {
List<T> children = treeMap.get(parentId);
if (children == null) {
return null;
}
for (Node item : children) {
item.setChildren(getTreeByMap(treeMap, item.getId()));
}
return children;
}
public static <T extends TreeEntity> List<T> convertListToTree(List<T> list) {
if (null == list || list.isEmpty()) {
return null;
}
// 存储数据到map中,key为节点的id
// Map<Long, TreeEntity> map = list.stream().collect(Collectors.toMap(key -> key.getId(), val -> val));
Map<Long, T> map = list.stream().collect(Collectors.toMap(TreeEntity::getId, val -> val));
return list.stream().filter(currentEntity -> {
// 父节点
Long parentId = currentEntity.getParentId();
// 从map中获取父节点的节点信息
TreeEntity parentEntity = map.get(parentId);
// 如果未找到节点
if (null == parentEntity) {
return true;
}
// 如果未找到父节点或者父节点为0,null,则为根节点
if (null == parentId || 0 == parentId) {
return true;
} else {
if (null == parentEntity.getChildren()) {
parentEntity.setChildren(new ArrayList());
}
// 把当前数据挂到父节点下
parentEntity.getChildren().add(currentEntity);
return false;
}
}).collect(Collectors.toList());
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论