I thinks you need DFS Algorithm and this implementation examples.... (https://www.cis.upenn.edu/~matuszek/cit594-2003/Examples/TreeTraversals/TreeTraversals.java)
But You can pass this method all Category and get tree of this
Category. And additionally I want to mention that, you can use CategoryDTO and relative CategoryMapper to represent better style on response (on representation layer e.g. - restful)
// for readability setters, getters and others fields (and code parts) are omitted
public class Category implements Serializable, Comparable<Category> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "created_on")
private Instant createdOn;
@Column(name = "created_by")
private String createdBy;
@Column(name = "status")
private Integer status;
@Column(name = "name")
private String name;
@ManyToOne(fetch = FetchType.LAZY)
private Category parent;
@Transient
private List<Category> child = new ArrayList<>();
}
this is method which returns list of Category tree
private List<Category> createTreeCategoriesByParent(final List<Category> categories, Long parentId) {
List<Category> siblings;
siblings = new ArrayList<>();
categories.forEach(category -> {
if (Objects.equals(category.getParent() == null ? null : category.getParent().getId(), parentId)) {
List<Category> children = createTreeCategoriesByParent(categories, category.getId());
children.sort(Category::compareTo);
category.setChild(children);
siblings.add(category);
}
});
return siblings;
}
Your input Category will
[
{
"status": 1,
"name": "Woman",
"accOrder": 0,
"parentId": null,
"child": []
},
{
"status": 1,
"name": "Man",
"accOrder": 0,
"parentId": null,
"child": []
},
{
"status": 1,
"name": "Shoes",
"accOrder": 0,
"parentId": 1,
"child": []
},
{
"status": 1,
"name": "Bijoux",
"accOrder": 1,
"parentId": 1,
"child": []
},
{
"status": 1,
"name": "Sneckers",
"accOrder": 1,
"parentId": 3,
"child": []
},
{
"status": 0,
"name": "Kids",
"accOrder": 1,
"parentId": null,
"child": []
},
{
"status": 1,
"name": "Good Sneckers",
"accOrder": 1,
"parentId": 5,
"child": []
},
{
"status": 1,
"name": "Bad Snackers",
"accOrder": 2,
"parentId": 5,
"child": []
}]
Output is
[
{
"status": null,
"name": "Woman",
"accOrder": 0,
"parentId": null,
"child": [
{
"status": null,
"name": "Shoes",
"accOrder": 0,
"parentId": 1,
"child": [
{
"status": null,
"name": "Sneckers",
"accOrder": 1,
"parentId": 3,
"child": [
{
"createdBy": null,
"status": null,
"name": "Good Sneckers",
"accOrder": 1,
"parentId": 5,
"child": []
},
{
"status": null,
"name": "Bad Snackers",
"accOrder": 2,
"parentId": 5,
"child": []
}
]
}
]
},
{
"status": null,
"name": "Bijoux",
"accOrder": 1,
"parentId": 1,
"child": []
}
]
},
{
"status": null,
"name": "Man",
"accOrder": 0,
"parentId": null,
"child": [
{
"status": null,
"name": "T-shirt",
"accOrder": 1,
"parentId": 2,
"child": []
}
]
},
{
"status": null,
"name": "Kids",
"accOrder": 1,
"parentId": null,
"child": []
},
{
"status": null,
"name": "Furniture",
"accOrder": 4,
"parentId": null,
"child": []
}
]