Java general three-level menu tool class
Usually, when writing a third-level menu, you will use recursive methods to write, but over time, you will find that many duplicate codes are being written and changed, and only a few names are changed.
Implementation method
Abstract property structure
The three commonly used fields are child id, parent id, and the second is array children.
This class will be implemented by returning an object or a third-level menu object.
import ; public interface AbstractTreeNode { // Child id Long getId(); // Parent id Long getParentId(); // Child array void setChildren(List<? extends AbstractTreeNode> children); }
Code Example
import ; import ; import ; @Data public class ForumBoardVo implements AbstractTreeNode { private Long id; // Parent section ID private Long parentId; // Section name private String boardName; // Cover private String cover; // describe private String boardDesc; // Sort private Integer sort; // 0: Only administrators are allowed to post 1: Anyone can post private Boolean postType; private List<ForumBoardVo> children; @Override public void setChildren(List<? extends AbstractTreeNode> children) { = (List<ForumBoardVo>) children; } }
Building a tree structure
import ; import ; public class TreeBuilder<T extends AbstractTreeNode> { /** * Build attribute structure * * @param nodeList The array to be built * @return Tree structure */ public List<T> buildTree(List<T> nodeList) { List<T> tree = new ArrayList<>(); for (T node : nodeList) { if (() == 0) { (getChildren((), nodeList)); (node); } } return tree; } /** * Recursively set the child menu * * @param nodeId nodeid * @param nodeList node list * @return child menu */ private List<T> getChildren(Long nodeId, List<T> nodeList) { List<T> children = new ArrayList<>(); for (T node : nodeList) { if (().equals(nodeId)) { (getChildren((), nodeList)); (node); } } return children; } }
First, return the array to normal. After all, we want to return the VO object. If we directly return the object in the database, it is also OK. Let's only give an example below.
public List<ForumBoardVo> loadBoard() { // list in the database List<ForumBoard> forumBoardListDB = list(); // Build return object array List<ForumBoardVo> forumBoardVoList = new ArrayList<>(); // Tree structure returns an array TreeBuilder<ForumBoardVo> treeBuilder = new TreeBuilder<>(); // Sort the arrays in the database, this is optional ((ForumBoard::getSort)); // Fill the arrays in the database into the array of List<ForumBoardVo> one by one (forumBoard -> { ForumBoardVo forumBoardVo = new ForumBoardVo(); (forumBoard, forumBoardVo); (forumBoardVo); }); // Finally, the tree structure object will be called to build a tree structure. return (forumBoardVoList); }
This is the end of this article about Java's general three-level menu tools. For more related Java three-level menu content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!