src/com/shop/util/CategoryTree.java

(开头部分) 4KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

package com.shop.util;

import com.shop.po.OsCategory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CategoryTree {
    private List<CategoryNode> treeList;
    private Map<Long, CategoryNode> tree = new HashMap<Long, CategoryNode>();

    public CategoryTree() {
    }

    public CategoryTree(List<CategoryNode> treeList) {
        this.treeList = treeList;
        this.buildTree();
    }

    private void buildTree() {
        CategoryNode root = new CategoryNode();
        tree.put(1L, root);
        for (int i = 0; i < treeList.size(); i++) {
            CategoryNode node = (CategoryNode) treeList.get(i);
            if ("0".equals(node.getParentId())) {
                root.addChild(node);
                tree.put(node.getCategoryId(), node);
            } else {
                if (tree.containsKey(node.getParentId())) {
                    CategoryNode parentNode = (CategoryNode) tree.get(node.getParentId());
                    parentNode.addChild(node);
                    tree.put(node.getCategoryId(), node);
                } else {
                    tree.put(node.getCategoryId(), node);
                }
            }
        }
    }

    public CategoryNode getTree() {
        CategoryNode node = (CategoryNode) tree.get(1L);
        return node;
    }

    public static void main(String[] args) {
        List<CategoryNode> nodes = new ArrayList<CategoryNode>();
        nodes.add(new CategoryNode(1L, 0L, "全部商品"));
        nodes.add(new CategoryNode(2L, 1L, "手机"));
        nodes.add(new CategoryNode(3L, 1L, "智能硬件"));
        nodes.add(new CategoryNode(4L, 1L, "笔记本  平板"));
        nodes.add(new CategoryNode(5L, 1L, "路由器  移动电源"));
        nodes.add(new CategoryNode(6L, 1L, "周边配件"));
        nodes.add(new CategoryNode(7L, 1L, "耳机  音响"));
        nodes.add(new CategoryNode(8L, 1L, "保护套  贴膜"));
        nodes.add(new CategoryNode(9L, 1L, "生活周边"));
        nodes.add(new CategoryNode(10L, 3L, "手环及配件"));
        nodes.add(new CategoryNode(11L, 3L, "智能灯"));
        nodes.add(new CategoryNode(12L, 3L, "智能家居"));
后面还有 57 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。