欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實現樹形菜單的方法總結

 更新時間:2023年08月30日 10:46:40   作者:一只愛擼貓的程序猿  
當我們想要展示層級結構,如文件目錄、組織結構或分類目錄時,樹形菜單是一個直觀且有效的解決方案,本文為大家整理了java中幾種常見方法,希望對大家有所幫助

當我們想要展示層級結構,如文件目錄、組織結構或分類目錄時,樹形菜單是一個直觀且有效的解決方案。在Java中,有多種方式可以實現樹形菜單,本文將介紹其中幾種常見方法,并配有示例代碼。

1. 使用遞歸法

遞歸法是最直觀的實現樹形菜單的方法。我們先定義節(jié)點結構,然后通過遞歸方式構建每一個節(jié)點的子節(jié)點。

實例代碼

class TreeNode {
    int id;
    int parentId;
    String name;
    List<TreeNode> children;
    // 構造函數、getters、setters省略
}
public List<TreeNode> buildTree(List<TreeNode> nodes, int parentId) {
    List<TreeNode> tree = new ArrayList<>();
    for (TreeNode node : nodes) {
        if (node.parentId == parentId) {
            node.children = buildTree(nodes, node.id);
            tree.add(node);
        }
    }
    return tree;
}

2. 使用隊列法 (層次遍歷)

使用隊列實現樹形菜單可以有效減少遞歸的深度,特別是在樹形結構很深的情況下。

實例代碼

public List<TreeNode> buildTreeWithQueue(List<TreeNode> nodes) {
    if (nodes == null || nodes.isEmpty()) {
        return Collections.emptyList();
    }
    Map<Integer, TreeNode> nodeMap = nodes.stream().collect(Collectors.toMap(TreeNode::getId, node -> node));
    List<TreeNode> tree = new ArrayList<>();
    Queue<TreeNode> queue = new LinkedList<>(nodes);
    while (!queue.isEmpty()) {
        TreeNode node = queue.poll();
        if (node.parentId == 0) {
            tree.add(node);
        } else {
            TreeNode parent = nodeMap.get(node.parentId);
            if (parent.children == null) {
                parent.children = new ArrayList<>();
            }
            parent.children.add(node);
        }
    }
    return tree;
}

3. 使用Map索引優(yōu)化

通過使用Map進行索引,可以提高搜索效率,特別是當節(jié)點數量非常多時。

實例代碼

public List<TreeNode> buildTreeWithMap(List<TreeNode> nodes) {
    Map<Integer, List<TreeNode>> childrenMap = new HashMap<>();
    List<TreeNode> rootNodes = new ArrayList<>();
    for (TreeNode node : nodes) {
        if (node.parentId == 0) {
            rootNodes.add(node);
        } else {
            childrenMap
                .computeIfAbsent(node.parentId, k -> new ArrayList<>())
                .add(node);
        }
    }
    for (TreeNode rootNode : rootNodes) {
        rootNode.children = getChildren(rootNode.id, childrenMap);
    }
    return rootNodes;
}
private List<TreeNode> getChildren(int parentId, Map<Integer, List<TreeNode>> childrenMap) {
    List<TreeNode> children = childrenMap.get(parentId);
    if (children != null) {
        for (TreeNode child : children) {
            child.children = getChildren(child.id, childrenMap);
        }
    }
    return children;
}

4. 數據庫的樹形編碼的方式

步驟1: 創(chuàng)建數據庫表結構

首先,我們需要一個數據庫表來存儲節(jié)點數據:

CREATE TABLE tree_nodes (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    path VARCHAR(255) DEFAULT '',
    parent_id INT
);

其中,path字段用于存儲從根節(jié)點到當前節(jié)點的路徑,如1/3/5/。

步驟2: Java 數據模型

我們需要一個Java類來表示數據庫中的節(jié)點:

public class TreeNode {
    private int id;
    private String name;
    private String path;
    private int parentId;
    // Getter, Setter 和 Constructor
}

步驟3: 插入節(jié)點

插入一個新的節(jié)點時,我們需要計算它的路徑:

public void addNode(String name, int parentId, Connection connection) throws SQLException {
    String path;
    if (parentId == 0) {
        path = "/";
    } else {
        String parentPathQuery = "SELECT path FROM tree_nodes WHERE id = ?";
        try (PreparedStatement stmt = connection.prepareStatement(parentPathQuery)) {
            stmt.setInt(1, parentId);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                path = rs.getString("path") + parentId + "/";
            } else {
                throw new SQLException("Parent not found");
            }
        }
    }
    String insertQuery = "INSERT INTO tree_nodes (name, path, parent_id) VALUES (?, ?, ?)";
    try (PreparedStatement stmt = connection.prepareStatement(insertQuery)) {
        stmt.setString(1, name);
        stmt.setString(2, path);
        stmt.setInt(3, parentId);
        stmt.executeUpdate();
    }
}

步驟4: 獲取某節(jié)點的所有子節(jié)點

public List<TreeNode> getChildren(int nodeId, Connection connection) throws SQLException {
    List<TreeNode> children = new ArrayList<>();
    String pathQuery = "SELECT path FROM tree_nodes WHERE id = ?";
    String path;
    try (PreparedStatement stmt = connection.prepareStatement(pathQuery)) {
        stmt.setInt(1, nodeId);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            path = rs.getString("path") + nodeId + "/";
        } else {
            throw new SQLException("Node not found");
        }
    }
    String childrenQuery = "SELECT * FROM tree_nodes WHERE path LIKE ?";
    try (PreparedStatement stmt = connection.prepareStatement(childrenQuery)) {
        stmt.setString(1, path + "%");
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            TreeNode node = new TreeNode();
            node.setId(rs.getInt("id"));
            node.setName(rs.getString("name"));
            node.setPath(rs.getString("path"));
            node.setParentId(rs.getInt("parent_id"));
            children.add(node);
        }
    }
    return children;
}

總結

以上我們介紹了三種在Java中實現樹形菜單的方法,從基礎的遞歸法、隊列法到使用Map進行優(yōu)化。根據實際需求和數據量大小,你可以選擇合適的方法進行實現。不過,無論使用哪種方法,都要確保理解樹形結構的基礎原理和具體實現細節(jié),以確保代碼的正確性和效率。 還有一種是路徑枚舉方式的一個簡單例子,它已經足夠用于實際應用。當然,還有許多細節(jié)可以優(yōu)化,如路徑長度限制、節(jié)點移動等。而其他的樹形編號方法,如嵌套集模型和閉包表,會有更復雜的邏輯。但總的來說,理解核心概念并將其應用于實際代碼是關鍵。

到此這篇關于Java實現樹形菜單的方法總結的文章就介紹到這了,更多相關Java樹形菜單內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java基于中介者模式實現多人聊天室功能示例

    Java基于中介者模式實現多人聊天室功能示例

    這篇文章主要介紹了Java基于中介者模式實現多人聊天室功能,詳細分析了中介者模式的概念、原理以及使用中介模式實現多人聊天的步驟、操作技巧與注意事項,需要的朋友可以參考下
    2018-05-05
  • 詳解Mybatis中的 ${} 和 #{}區(qū)別與用法

    詳解Mybatis中的 ${} 和 #{}區(qū)別與用法

    這篇文章主要介紹了Mybatis中的 ${} 和 #{}區(qū)別與用法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • struts2 validation.xml 驗證規(guī)則代碼解析

    struts2 validation.xml 驗證規(guī)則代碼解析

    這篇文章主要介紹了struts2 validation.xml 驗證規(guī)則代碼解析,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Java獲取當前時間年月日的方法

    Java獲取當前時間年月日的方法

    這篇文章主要為大家詳細介紹了Java獲取當前時間年月日的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 使用log4j2關閉debug日志

    使用log4j2關閉debug日志

    這篇文章主要介紹了使用log4j2關閉debug日志方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Mybatis-Plus自動填充更新操作相關字段的實現

    Mybatis-Plus自動填充更新操作相關字段的實現

    這篇文章主要介紹了Mybatis-Plus自動填充更新操作相關字段的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • Java中BigDecimal類的簡單用法

    Java中BigDecimal類的簡單用法

    這篇文章主要介紹了Java中BigDecimal類的簡單用法,是Java應用程序開發(fā)中非常實用的技巧,本文以實例形式對此進行了簡單的分析,需要的朋友可以參考下
    2014-09-09
  • SpringBoot中Excel處理完全指南分享

    SpringBoot中Excel處理完全指南分享

    這篇文章主要介紹了SpringBoot中Excel處理完全指南,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • spring mvc中的@PathVariable獲得請求url中的動態(tài)參數

    spring mvc中的@PathVariable獲得請求url中的動態(tài)參數

    本文主要介紹了spring mvc中的@PathVariable獲得請求url中的動態(tài)參數的代碼。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • SpringBoot同時集成Mybatis和Mybatis-plus框架

    SpringBoot同時集成Mybatis和Mybatis-plus框架

    在實際開發(fā)中,項目里面一般都是Mybatis和Mybatis-Plus公用,但是公用有版本不兼容的問題,本文主要介紹了Spring Boot項目中同時集成Mybatis和Mybatis-plus,具有一檔的參考價值,感興趣的可以了解一下
    2024-12-12

最新評論