[Java 实现三级类目树 demo ]

目录

前言 :

下面是示例根据类目id 查询类目树:

说明:

Java接口 直接返回list 三级类目树列表 

说明:

Java代码示例,用于根据传参二级类目id查询下一级的类目结构:

说明:


前言 :

     小笔记 提供大家参考 , 有帮助的话 那就更好啦 !  赠人玫瑰,手有余香

下面是示例根据类目id 查询类目树:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class CategoryTreeDemo {private Map categoryMap = new HashMap<>();private List rootCategories = new ArrayList<>();// 初始化三级类目树public void init() {// 模拟数据库中的数据Category category1 = new Category("1", "数码产品", "0", "0");Category category2 = new Category("2", "智能手机", "1", "0");Category category3 = new Category("3", "笔记本电脑", "1", "0");Category category4 = new Category("4", "电视机", "1", "0");Category category5 = new Category("5", "苹果手机", "2", "0");Category category6 = new Category("6", "三星手机", "2", "0");Category category7 = new Category("7", "ThinkPad", "3", "0");Category category8 = new Category("8", "华为笔记本", "3", "0");Category category9 = new Category("9", "小米电视", "4", "0");Category category10 = new Category("10", "创维电视", "4", "0");// 保存到map中categoryMap.put(category1.getId(), category1);categoryMap.put(category2.getId(), category2);categoryMap.put(category3.getId(), category3);categoryMap.put(category4.getId(), category4);categoryMap.put(category5.getId(), category5);categoryMap.put(category6.getId(), category6);categoryMap.put(category7.getId(), category7);categoryMap.put(category8.getId(), category8);categoryMap.put(category9.getId(), category9);categoryMap.put(category10.getId(), category10);// 构建三级类目树for (Category category : categoryMap.values()) {if ("0".equals(category.getPid())) {// 找到根节点rootCategories.add(category);} else {// 找到父节点Category parentCategory = categoryMap.get(category.getPid());if (parentCategory != null) {// 将当前节点添加到父节点的子节点列表中parentCategory.getChildren().add(category);}}}}// 根据类目id查询三级类目树public Category getTree(String categoryId) {Category category = categoryMap.get(categoryId);if (category == null) {return null;}// 如果当前节点不是根节点,则递归查找其父节点,直到找到根节点while (!"0".equals(category.getPid())) {category = categoryMap.get(category.getPid());}return category;}public static void main(String[] args) {CategoryTreeDemo demo = new CategoryTreeDemo();demo.init();Category category = demo.getTree("5");System.out.println(category);}
}class Category {private String id;private String name;private String pid;private String level;private List children = new ArrayList<>();public Category(String id, String name, String pid, String level) {this.id = id;this.name = name;this.pid = pid;this.level = level;}// get/set方法省略...@Overridepublic String toString() {return "Category{" +"id='" + id + ''' +", name='" + name + ''' +", pid='" + pid + ''' +", level='" + level + ''' +", children=" + children +'}';}
}

说明:

  1. 首先在init()方法中,模拟了一个三级类目树的数据库表,并将其保存到一个Map中。在保存时,将每个类目的id作为key,类目对象作为value。
  2. 然后在构建三级类目树时,遍历Map中的每个类目对象,将根节点和子节点分别保存到rootCategories和children列表中。具体的代码逻辑如下:
    1. 如果当前类目对象的pid为0,则将其添加到rootCategories中。
    2. 如果当前类目对象的pid不为0,则根据pid从Map中获取其父

Java接口 直接返回list 三级类目树列表 

用于直接返回三级类目树的列表,不需要传参获取类目结构:

import java.util.ArrayList;
import java.util.List;public class CategoryTreeListDemo {// 初始化三级类目树public List init() {// 模拟数据库中的数据Category category1 = new Category("1", "数码产品", "0", "0");Category category2 = new Category("2", "智能手机", "1", "0");Category category3 = new Category("3", "笔记本电脑", "1", "0");Category category4 = new Category("4", "电视机", "1", "0");Category category5 = new Category("5", "苹果手机", "2", "0");Category category6 = new Category("6", "三星手机", "2", "0");Category category7 = new Category("7", "ThinkPad", "3", "0");Category category8 = new Category("8", "华为笔记本", "3", "0");Category category9 = new Category("9", "小米电视", "4", "0");Category category10 = new Category("10", "创维电视", "4", "0");List categories = new ArrayList<>();categories.add(category1);categories.add(category2);categories.add(category3);categories.add(category4);categories.add(category5);categories.add(category6);categories.add(category7);categories.add(category8);categories.add(category9);categories.add(category10);// 构建三级类目树List resultList = new ArrayList<>();for (Category category : categories) {if ("0".equals(category.getPid())) {// 找到根节点resultList.add(category);for (Category subCategory1 : categories) {if (subCategory1.getPid().equals(category.getId())) {// 找到第一级子节点resultList.add(subCategory1);for (Category subCategory2 : categories) {if (subCategory2.getPid().equals(subCategory1.getId())) {// 找到第二级子节点resultList.add(subCategory2);}}}}}}return resultList;}public static void main(String[] args) {CategoryTreeListDemo demo = new CategoryTreeListDemo();List categoryList = demo.init();System.out.println(categoryList);}
}class Category {private String id;private String name;private String pid;private String level;public Category(String id, String name, String pid, String level) {this.id = id;this.name = name;this.pid = pid;this.level = level;}// get/set方法省略...@Overridepublic String toString() {return "Category{" +"id='" + id + ''' +", name='" + name + ''' +", pid='" + pid + ''' +", level='" + level + ''' +'}';}
}

说明:

  • 在init()方法中,首先模拟了一个三级类目树的列表,并将其保存到一个List中。
  • 然后在构建三级类目树时,遍历列表中的每个类目对象,将根节点、第一级子节点和第二级子节点分别保存到resultList中。具体的代码逻辑如下:
    • 如果当前类目对象的pid为0,则将其作为根节点,然后继续遍历列表,找到其下面的子节点。
    • 如果当前类目对象的pid等于根节点的id,说明当前类目是根节点的子节点,将其作为第一级子节点,然后继续遍历列表,找到其下面的子节点。
    • 如果当前类目对象的pid等于第一级子节点的id,说明当前类目是第一级子节点的子节点,将其作为第二级子节点。
  • 最后在main方法中,调用init()方法,获取三级类目树的列表并打印输出。

Java代码示例,用于根据传参二级类目id查询下一级的类目结构:

import java.util.ArrayList;
import java.util.List;public class CategoryTreeDemo {// 初始化三级类目树public List init() {// 模拟数据库中的数据Category category1 = new Category("1", "数码产品", "0", "0");Category category2 = new Category("2", "智能手机", "1", "0");Category category3 = new Category("3", "笔记本电脑", "1", "0");Category category4 = new Category("4", "电视机", "1", "0");Category category5 = new Category("5", "苹果手机", "2", "0");Category category6 = new Category("6", "三星手机", "2", "0");Category category7 = new Category("7", "ThinkPad", "3", "0");Category category8 = new Category("8", "华为笔记本", "3", "0");Category category9 = new Category("9", "小米电视", "4", "0");Category category10 = new Category("10", "创维电视", "4", "0");List categories = new ArrayList<>();categories.add(category1);categories.add(category2);categories.add(category3);categories.add(category4);categories.add(category5);categories.add(category6);categories.add(category7);categories.add(category8);categories.add(category9);categories.add(category10);// 构建三级类目树List resultList = new ArrayList<>();for (Category category : categories) {if ("0".equals(category.getPid())) {// 找到根节点resultList.add(category);for (Category subCategory1 : categories) {if (subCategory1.getPid().equals(category.getId())) {// 找到第一级子节点resultList.add(subCategory1);for (Category subCategory2 : categories) {if (subCategory2.getPid().equals(subCategory1.getId())) {// 找到第二级子节点resultList.add(subCategory2);}}}}}}return resultList;}// 根据二级类目id查询下一级类目结构public List getNextLevel(String categoryId) {List categories = init();List resultList = new ArrayList<>();// 查找二级类目for (Category category : categories) {if (categoryId.equals(category.getId())) {// 找到二级类目的子节点for (Category subCategory : category.getChildren()) {resultList.add(subCategory);}break;}}return resultList;}public static void main(String[] args) {CategoryTreeDemo demo = new CategoryTreeDemo();List categoryList = demo.getNextLevel("2");System.out.println(categoryList);}
}class Category {private String id;private String name;private String pid;private String level;private List children = new ArrayList<>();public Category(String id, String name, String pid, String level) {this.id = id;this.name = name;this.pid = pid;this.level = level;}// get/set方法省略...@Overridepublic String toString() {return "Category{" +"id='" + id + ''' +", name='" + name + ''' +", pid='" + pid + ''' +", level='" + level + ''' +", children=" + children +'}';}
}

说明:

  • 在init()方法中,首先模拟了一个三级类目树的列表,并将其保存到一个List中。
  • 然后在构建三级类目树时,遍历列表中的每个类目对象,将根节点、第一级子节点和第二级子节点分别保存到resultList中。具体的代码逻辑如上面的demo所示。
  • 在getNextLevel()方法中,先使用init()方法获取所有的类目结构,然后根据传入的二级类目id查找其子节点,将其添加到结果列表中。具体的代码逻辑如下:
    • 遍历所有类目对象,如果当前类目的id等于传入的类目id,说明找到了要查询的二级类目。
    • 遍历该二级类目对象的子节点,将其添加到结果列表中。
  • 最后在main方法中,调用getNextLevel()方法,传入二级类目id参数,获取下一级的类目结构并打印输出。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部