当前位置: 首页 > news >正文

爱站网 关键词挖掘工具站长工具网站建设+荆州

爱站网 关键词挖掘工具站长工具,网站建设+荆州,编写程序的步骤,wordpress怎么样一、需求 项目中有个树形列表(三层结构),想根据结构中的名称进行搜索,输出的结果依然保持树形结构。结构如下: 二、代码 import lombok.Data;import java.time.LocalDateTime; import java.util.ArrayList; import…

一、需求

项目中有个树形列表(三层结构),想根据结构中的名称进行搜索,输出的结果依然保持树形结构。结构如下:
在这里插入图片描述

二、代码

import lombok.Data;import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;@Data
public class ProductCategoryRespVO {private Long id;private Long parentId;private String name;private String picUrl;private Integer sort;private Integer status;private String description;private LocalDateTime createTime;private Integer level;// 子分类列表private List<ProductCategoryRespVO> children = new ArrayList<>();public ProductCategoryRespVO(Long id, Long parentId, String name, String picUrl, Integer sort, Integer status, String description, LocalDateTime createTime, Integer level) {this.id = id;this.parentId = parentId;this.name = name;this.picUrl = picUrl;this.sort = sort;this.status = status;this.description = description;this.createTime = createTime;this.level = level;this.children = new ArrayList<>();}public void addChild(ProductCategoryRespVO child) {this.children.add(child);}
}

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;public class TreeUtil {// 搜索上下文类,用于存储搜索路径static class SearchContext {ProductCategoryRespVO level1Node = null;ProductCategoryRespVO level2Node = null;ProductCategoryRespVO matchedNode = null;}// 处理分类数组的搜索方法public static List<ProductCategoryRespVO> searchCategoryList(List<ProductCategoryRespVO> categories, String searchName) {if (CollectionUtils.isEmpty(categories) || StringUtils.isEmpty(searchName)) {return new ArrayList<>();}List<ProductCategoryRespVO> resultList = new ArrayList<>();for (ProductCategoryRespVO root : categories) {ProductCategoryRespVO result = searchCategoryTree(root, searchName);if (result != null) {resultList.add(result);}}return resultList;}// 搜索单个树的方法public static ProductCategoryRespVO searchCategoryTree(ProductCategoryRespVO root, String searchName) {SearchContext context = new SearchContext();// 查找匹配节点并记录路径findMatchedNode(root, searchName, context);// 根据匹配节点的层级返回不同的树形结构if (context.matchedNode == null) {return null; // 未找到匹配节点}if (context.matchedNode.getLevel() == 1) {// 命中level1节点,返回完整节点及其子节点return cloneNodeWithChildren(context.matchedNode);} else if (context.matchedNode.getLevel() == 2) {// 命中level2节点,返回其父节点(level1)和自身及其子节点ProductCategoryRespVO level1Clone = cloneNodeWithoutChildren(context.level1Node);ProductCategoryRespVO level2Clone = cloneNodeWithChildren(context.matchedNode);level1Clone.getChildren().add(level2Clone);return level1Clone;} else if (context.matchedNode.getLevel() == 3) {// 命中level3节点,返回其祖父节点(level1)、父节点(level2)和自身ProductCategoryRespVO level1Clone = cloneNodeWithoutChildren(context.level1Node);ProductCategoryRespVO level2Clone = cloneNodeWithoutChildren(context.level2Node);ProductCategoryRespVO level3Clone = cloneNodeWithChildren(context.matchedNode);level2Clone.getChildren().add(level3Clone);level1Clone.getChildren().add(level2Clone);return level1Clone;}return null;}// 递归查找匹配节点并记录路径private static void findMatchedNode(ProductCategoryRespVO node, String searchName, SearchContext context) {if (node == null) return;// 记录路径if (node.getLevel() == 1) {context.level1Node = node;context.level2Node = null; // 重置level2节点,因为进入了新的level1节点} else if (node.getLevel() == 2) {context.level2Node = node;}//        // 检查当前节点是否匹配
//        if (node.getName().equals(searchName)) {
//            context.matchedNode = node;
//            return;
//        }// 检查当前节点是否匹配if (node.getName().contains(searchName)) {context.matchedNode = node;return;}// 递归搜索子节点for (ProductCategoryRespVO child : node.getChildren()) {findMatchedNode(child, searchName, context);// 如果已经找到匹配节点,直接返回if (context.matchedNode != null) {return;}}}// 克隆节点但不包含子节点private static ProductCategoryRespVO cloneNodeWithoutChildren(ProductCategoryRespVO original) {ProductCategoryRespVO clone = new ProductCategoryRespVO(original.getId(),original.getParentId(),original.getName(),original.getPicUrl(),original.getSort(),original.getStatus(),original.getDescription(),original.getCreateTime(),original.getLevel());return clone;}// 克隆节点及其所有子节点private static ProductCategoryRespVO cloneNodeWithChildren(ProductCategoryRespVO original) {ProductCategoryRespVO clone = cloneNodeWithoutChildren(original);for (ProductCategoryRespVO child : original.getChildren()) {clone.addChild(cloneNodeWithChildren(child));}return clone;}// 示例用法public static void main(String[] args) {// 构建示例树结构 - 女装ProductCategoryRespVO womenRoot = new ProductCategoryRespVO(16L, 0L, "女装", "url", 1, 0, null, LocalDateTime.now(), 1);ProductCategoryRespVO womenTop = new ProductCategoryRespVO(17L, 16L, "上衣", "url", 1, 0, null, LocalDateTime.now(), 2);ProductCategoryRespVO womenVest = new ProductCategoryRespVO(21L, 17L, "背心", "url", 1, 0, null, LocalDateTime.now(), 3);ProductCategoryRespVO womenCoat = new ProductCategoryRespVO(23L, 17L, "外套", "url", 1, 0, null, LocalDateTime.now(), 3);womenRoot.addChild(womenTop);womenTop.addChild(womenVest);womenTop.addChild(womenCoat);// 构建示例树结构 - 男装ProductCategoryRespVO menRoot = new ProductCategoryRespVO(163L, 0L, "男装", "url", 1, 0, null, LocalDateTime.now(), 1);ProductCategoryRespVO menTop = new ProductCategoryRespVO(173L, 163L, "上衣", "url", 1, 0, null, LocalDateTime.now(), 2);ProductCategoryRespVO menVest = new ProductCategoryRespVO(213L, 173L, "背心", "url", 1, 0, null, LocalDateTime.now(), 3);ProductCategoryRespVO menCoat = new ProductCategoryRespVO(233L, 173L, "外套", "url", 1, 0, null, LocalDateTime.now(), 3);menRoot.addChild(menTop);menTop.addChild(menVest);menTop.addChild(menCoat);// 创建分类数组List<ProductCategoryRespVO> categoryList = new ArrayList<>();categoryList.add(womenRoot);categoryList.add(menRoot);// 搜索示例System.out.println("搜索 '女装':");List<ProductCategoryRespVO> result1 = searchCategoryList(categoryList, "女装");printResultList(result1);System.out.println("\n搜索 '上衣':");List<ProductCategoryRespVO> result2 = searchCategoryList(categoryList, "上衣");printResultList(result2);System.out.println("\n搜索 '背心':");List<ProductCategoryRespVO> result3 = searchCategoryList(categoryList, "背心");printResultList(result3);}// 辅助方法:打印搜索结果列表private static void printResultList(List<ProductCategoryRespVO> resultList) {if (resultList.isEmpty()) {System.out.println("未找到匹配的分类");return;}for (ProductCategoryRespVO root : resultList) {printTree(root, 0);System.out.println(); // 空行分隔不同的树}}// 辅助方法:打印树结构private static void printTree(ProductCategoryRespVO node, int level) {if (node == null) return;StringBuilder indent = new StringBuilder();for (int i = 0; i < level; i++) {indent.append("  ");}System.out.println(indent + node.getName() + " (Level " + node.getLevel() + ")");for (ProductCategoryRespVO child : node.getChildren()) {printTree(child, level + 1);}}}

三、测试结果

1、搜索“女装”,展示全部,结果如下:
在这里插入图片描述
2、搜索“裙装”,,展示“裙装”的上下节点数据,结果如下:
在这里插入图片描述
3、搜索“T恤”,,展示“T恤”的上下节点数据,结果如下:
在这里插入图片描述

http://www.dtcms.com/a/506278.html

相关文章:

  • 做二手房比较好的网站有哪些赤水市住房和城乡建设局网站
  • 做网站推广的技巧西安网站建站品牌
  • 做网站推广销售怎么样桃城网站建设代理
  • html5手机网站织梦模板wordpress 首页慢
  • c 网站开发入门视频网站空间控制面板
  • 手机网站开发计划佛山高端网站制作
  • 网站制作职业wordpress商城教程
  • 上海买二手房做哪个网站好浏览器推广怎么做
  • 公司做的网站计入什么前端网站做中 英文怎么说
  • 路由器带u盘接口的做网站成都旅游必去的地方
  • 特色的佛山网站建设青岛外贸网站建站
  • 炫酷响应式网站设计wordpress百度推送插件
  • 北京网站提升排名wordpress 招聘模块
  • 成都网站建设电话买个域名
  • iis7.5 网站打不开请小组讨论一个完整的网页设计流程
  • 怎么查网站域名中国建筑工程网施工资料
  • 百度做网站效果怎么样金华建设局网站
  • 蓝天网站建设加盟型网站建设
  • 网站的管理维护徐州 商城网站设计
  • 如何建立网站建设单位做网站图片素材
  • 淄博网站制作公司推广赶集网免费发布信息网
  • 网站做rss+wordpress中国网络科技公司排名
  • 深圳设计功能网站WordPress开发微信支付
  • 大型网站响应式济宁创企网络技术有限公司
  • 盐城网站建设案例网页视频下载器手机版
  • 北京建网站价格优帮云网站建设怎么创业
  • 翡翠网站建设ppt网络安全工程师是干嘛的
  • 网站诊断与优化的作用黄岛网站建设哪家权威
  • 装修公司网站php源码apache 多网站
  • 想找人做网站怎么找到换域名的网站