Java构建树结构工具类
import java.util.ArrayList;
import java.util.List;
public interface TreeUtil {
interface Node<K, T extends Node<K, T>> {
void setId(K id);
K getId();
void setPid(K pid);
K getPid();
void setCount(int count);
int getCount();
void setChild(List<T> child);
List<T> getChild();
}
static <I, E extends Node<I, E>> void buildTree(List<E> src, Node<I, E> root) {
List<E> child = new ArrayList<>();
src.removeIf(item -> {
if (item.getPid().equals(root.getId())) {
child.add(item);
return true;
}
return false;
});
if (!child.isEmpty()) {
root.setChild(child);
root.setCount(child.size());
child.forEach(item -> buildTree(src, item));
}
}
static <M, N extends Node<M, N>> List<N> build(List<N> src, Class<N> clazz, M rid) {
Node<M, N> root;
try {
root = clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
root.setId(rid);
buildTree(src, root);
return root.getChild();
}
static <I, E extends Node<I, E>> List<E> buildTreeNoRecursion(List<E> src, I root) {
if (root == null) {
throw new IllegalArgumentException("根节点ID不能为空");
}
List<E> result = new LinkedList<>();
Map<I, E> map = src.stream()
.collect(Collectors.toMap(E::getId, e -> e));
for (E elem : src) {
if (Objects.equals(root, elem.getPid())) {
result.add(elem);
} else {
E e = map.get(elem.getPid());
if (e == null) {
continue;
}
List<E> child = e.getChild();
if (child == null) {
LinkedList<E> list = new LinkedList<>();
list.add(elem);
e.setChild(list);
} else {
e.getChild().add(elem);
}
}
}
return result;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class BuilderUtil<T> {
private final Supplier<T> instantiate;
private final List<Consumer<T>> instanceModifiers = new ArrayList<>();
public BuilderUtil(Supplier<T> instantiate) {
this.instantiate = instantiate;
}
public static <T> BuilderUtil<T> of(Supplier<T> instantiator) {
return new BuilderUtil<T>(instantiator);
}
public <U> BuilderUtil<T> with(BiConsumer<T, U> consumer, U value) {
Consumer<T> c = instance -> consumer.accept(instance, value);
instanceModifiers.add(c);
return this;
}
public T build() {
T value = instantiate.get();
instanceModifiers.forEach(modifier -> modifier.accept(value));
instanceModifiers.clear();
return value;
}
}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.omega.demo.utils.BuilderUtil;
import com.omega.demo.utils.TreeUtil;
import java.util.ArrayList;
import java.util.List;
public class BuildTreeTest {
public static void main(String[] args) {
List<TreeNode> src = new ArrayList<>();
src.add(BuilderUtil.of(TreeNode::new).with(TreeNode::setId, 50066159978024960L).with(TreeNode::setName, "四川省").with(TreeNode::setPid, 0L).build());
src.add(BuilderUtil.of(TreeNode::new).with(TreeNode::setId, 50066159978024961L).with(TreeNode::setName, "重庆市").with(TreeNode::setPid, 0L).build());
src.add(BuilderUtil.of(TreeNode::new).with(TreeNode::setId, 50066159978024962L).with(TreeNode::setName, "山西省").with(TreeNode::setPid, 0L).build());
src.add(BuilderUtil.of(TreeNode::new).with(TreeNode::setId, 50066159978024963L).with(TreeNode::setName, "成都市").with(TreeNode::setPid, 50066159978024960L).build());
src.add(BuilderUtil.of(TreeNode::new).with(TreeNode::setId, 50066159978024964L).with(TreeNode::setName, "成都01").with(TreeNode::setPid, 50066159978024963L).build());
src.add(BuilderUtil.of(TreeNode::new).with(TreeNode::setId, 50066159978024965L).with(TreeNode::setName, "成都02").with(TreeNode::setPid, 50066159978024963L).build());
src.add(BuilderUtil.of(TreeNode::new).with(TreeNode::setId, 50066159978024966L).with(TreeNode::setName, "重庆市").with(TreeNode::setPid, 50066159978024961L).build());
src.add(BuilderUtil.of(TreeNode::new).with(TreeNode::setId, 50066159978024967L).with(TreeNode::setName, "重庆01").with(TreeNode::setPid, 50066159978024966L).build());
src.add(BuilderUtil.of(TreeNode::new).with(TreeNode::setId, 50066159978024968L).with(TreeNode::setName, "重庆02").with(TreeNode::setPid, 50066159978024966L).build());
src.add(BuilderUtil.of(TreeNode::new).with(TreeNode::setId, 50066159978024969L).with(TreeNode::setName, "太原市").with(TreeNode::setPid, 50066159978024962L).build());
src.add(BuilderUtil.of(TreeNode::new).with(TreeNode::setId, 50066159978024970L).with(TreeNode::setName, "太远01").with(TreeNode::setPid, 50066159978024969L).build());
src.add(BuilderUtil.of(TreeNode::new).with(TreeNode::setId, 50066159978024971L).with(TreeNode::setName, "太远02").with(TreeNode::setPid, 50066159978024969L).build());
List<TreeNode> tree = TreeUtil.build(src, TreeNode.class, 0L);
ObjectMapper objectMapper = new ObjectMapper();
String str;
try {
str = objectMapper.writeValueAsString(tree);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
System.out.println(str);
}
}
[
{
"id":50066159978024960,
"name":"四川省",
"pid":0,
"count":1,
"child":[
{
"id":50066159978024960,
"name":"成都市",
"pid":50066159978024960,
"count":2,
"child":[
{
"id":50066159978024960,
"name":"成都01",
"pid":50066159978024960,
"count":0,
"child":null
},
{
"id":50066159978024970,
"name":"成都02",
"pid":50066159978024960,
"count":0,
"child":null
}
]
}
]
},
{
"id":50066159978024960,
"name":"重庆市",
"pid":0,
"count":1,
"child":[
{
"id":50066159978024970,
"name":"重庆市",
"pid":50066159978024960,
"count":2,
"child":[
{
"id":50066159978024970,
"name":"重庆01",
"pid":50066159978024970,
"count":0,
"child":null
},
{
"id":50066159978024970,
"name":"重庆02",
"pid":50066159978024970,
"count":0,
"child":null
}
]
}
]
},
{
"id":50066159978024960,
"name":"山西省",
"pid":0,
"count":1,
"child":[
{
"id":50066159978024970,
"name":"太原市",
"pid":50066159978024960,
"count":2,
"child":[
{
"id":50066159978024970,
"name":"太远01",
"pid":50066159978024970,
"count":0,
"child":null
},
{
"id":50066159978024970,
"name":"太远02",
"pid":50066159978024970,
"count":0,
"child":null
}
]
}
]
}
]