Converting a List to a Tree Structure Using Java 8 Streams
This article demonstrates how to transform a flat list into a hierarchical tree structure in Java by defining a NodeVO class, implementing a recursive Stream‑based conversion method, and testing it with sample data, showcasing the elegance of the Stream API over traditional loops.
In everyday development, handling hierarchical data structures is common, and while MyBatis resultMap can be used for conversion, a manual Java approach is sometimes needed; this article explores using the Java 8 Stream API to convert a list into a tree structure.
1. Define the VO class for the front‑end response
import lombok.Data;
import java.util.List;
/**
* @author wangzhenjun
* @date 2022/2/28 10:28
*/
@Data
public class NodeVO {
private String id;
private String name;
private String pid;
private List
children;
public NodeVO(String id, String name, String pid) {
this.id = id;
this.name = name;
this.pid = pid;
}
}2. Implement the list‑to‑tree conversion using Stream
public static List
streamToTree(List
treeList, String parentId) {
List
list = treeList.stream()
// filter parent nodes
.filter(parent -> parent.getPid().equals(parentId))
// recursively set children for each parent
.map(child -> {
child.setChildren(streamToTree(treeList, child.getId()));
return child;
})
.collect(Collectors.toList());
return list;
}3. Test the conversion
public static void main(String[] args) {
NodeVO NodeVO1 = new NodeVO("1", "山东省", "0");
NodeVO NodeVO2 = new NodeVO("2", "青岛市", "1");
NodeVO NodeVO3 = new NodeVO("3", "市北区", "2");
NodeVO NodeVO4 = new NodeVO("4", "济南市", "1");
NodeVO NodeVO5 = new NodeVO("5", "浙江省", "0");
NodeVO NodeVO6 = new NodeVO("6", "杭州市", "5");
NodeVO NodeVO7 = new NodeVO("7", "西湖区", "6");
List
list = new ArrayList<>();
list.add(NodeVO1);
list.add(NodeVO2);
list.add(NodeVO3);
list.add(NodeVO4);
list.add(NodeVO5);
list.add(NodeVO6);
list.add(NodeVO7);
// default parent id is 0
List
nodeVOList = streamToTree(list, "0");
System.out.println(JSON.toJSONString(nodeVOList));
}4. Result output
[
{
"children": [
{
"children": [
{
"children": [],
"id": "3",
"name": "市北区",
"pid": "2"
}
],
"id": "2",
"name": "青岛市",
"pid": "1"
},
{
"children": [],
"id": "4",
"name": "济南市",
"pid": "1"
}
],
"id": "1",
"name": "山东省",
"pid": "0"
},
{
"children": [
{
"children": [
{
"children": [],
"id": "7",
"name": "西湖区",
"pid": "6"
}
],
"id": "6",
"name": "杭州市",
"pid": "5"
}
],
"id": "5",
"name": "浙江省",
"pid": "0"
}
]5. Summary
The Stream‑based approach provides a concise and elegant way to build hierarchical trees from flat lists, and although a simple double‑loop solution also works, the author prefers the readability and functional style of Java Streams.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.