Closed mr-mihu closed 1 month ago
JDK版本: openjdk_8_201 hutool版本: 5.8.25
public class Tree<T> extends LinkedHashMap<String, Object> implements Node<T> { /** * 扩展属性 * * @param field 字段 * @param value 扩展值 */ public <T, C> void putExtra(Func1<T, C> field, C value) { String key = getColumn(field); super.putExtra(key, value); } /** * 扩展属性 * * @param field 字段 * @return 返回值 */ @SuppressWarnings("unchecked") public <T, C> C getExtra(Func1<T, C> field) { String key = getColumn(field); Object o = super.getExtra(key); if (o == null) { return null; } return (C) o; } /** * 获取属性名称 * * @param func 字段lambda表达式 * @return java.lang.String */ public static <T, C> String getColumn(Func1<T, C> func) { //获取实体属性名称 String fieldName = LambdaUtil.getFieldName(func); if (StrUtil.isBlankIfStr(fieldName)) { return null; } //通过属性名获取对应的列名 return fieldName; } // 原本代码逻辑 ··· }
用法
long bookId = tree.get(BookDirDTO::getBookId));
List<Tree> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig, (treeNode, tree) -> { tree.setId(treeNode.getId()); tree.setParentId(treeNode.getParentId()); tree.setWeight(treeNode.getWeight()); tree.setName(treeNode.getName()); // 扩展属性 ... tree.putExtra(BookDirDTO::getBookId, 666L); tree.putExtra(BookDirDTO::getOtherId, new Object()); });
@VampireAchao 是不是6.x支持了?
6..0.0的BeanTree支持Lambda方式。
见:https://github.com/dromara/hutool/blob/v6-dev/hutool-core/src/test/java/org/dromara/hutool/core/tree/BeanTreeTest.java
版本情况
JDK版本: openjdk_8_201 hutool版本: 5.8.25
问题描述(包括截图)
用法
List<Tree> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
(treeNode, tree) -> {
tree.setId(treeNode.getId());
tree.setParentId(treeNode.getParentId());
tree.setWeight(treeNode.getWeight());
tree.setName(treeNode.getName());
// 扩展属性 ...
tree.putExtra(BookDirDTO::getBookId, 666L);
tree.putExtra(BookDirDTO::getOtherId, new Object());
});