JAVA根据条件检索对树进行筛选

根据条件过滤命中路径树

需求:根据条件检索获取当前数据的父节点路径

  树结构如下:                   命中节点:3、9、6、11        命中节点路径树:
			    
			      1                                                     1
			/     |    \                                             /  |  \
		   2      3      4                                          2   3  4
		  /  \   /  \   /  \                                       / \    /
		 5    6 7    8 9   10                                     5   6  9
	    / \  / \                                                 /
       11 12 13 14                                              11
	  / \       
	 15  16    

整体解决思路:

参数

  1. 树平铺的列表nodeList;
  2. 命中节点的集合ids;
  3. 接收命中节点所有路径的列表newNodes;

解决方案

  1. 根据命中节点的集合ids 获取命中节点的数据集合hitList
List<SearchTree > hitList= list.stream().filter(item -> ids.contains(item.getId())).collect(Collectors.toList());
  1. 对集合hitList进行遍历,遍历过程中,判断newNodes中是否包含了当前节点对象currentNode
    2.1、如果包含了,则继续往下遍历;
    2.2、如果不包含,将当前对象放入newNodes中;
    -------进入do…while:
    -------根据当前对象的pid获取父节点,并赋值给当前节点对象currentNode;
    -------判断newNodes中是否包含了当前节点对象currentNode(非空),
    -------2.2.1、如果包含了、则跳出当前循环;
    -------2.2.2、如果不包含、将当前对象放入newNodes中;
    -------2.2.3、判断当前节点对象currentNode的Pid是否为空串,
    -----------------如果为空则跳出do…while,否则继续do…while。
        for (SearchTree currentNode : hitList) {
            if (!newNodes.contains(currentNode)) {
                newNodes.add(currentNode);
                do {
                    currentNode = map.get(currentNode.getPid());
                    if (null != currentNode) {
                        if (newNodes.contains(currentNode)) break;
                        newNodes.add(currentNode);
                    }
                } while (!"".equals(currentNode.getPid()));
            }
        }

完整代码如下:

public static List<SearchTree> hitPathList(List<SearchTree> list, Set<String> ids) {
		List<SearchTree> newNodes = new ArrayList<>();
        List<SearchTree> hitList = list.stream().filter(item -> ids.contains(item.getId())).collect(Collectors.toList());
        Map<String, SearchTree> map = list.stream().collect(Collectors.toMap(SearchTree::getId, Function.identity()));
        for (SearchTree currentNode : hitList) {
            if (!newNodes.contains(currentNode)) {
                newNodes.add(currentNode);
                do {
                    currentNode = map.get(currentNode.getPid());
                    if (null != currentNode) {
                        if (newNodes.contains(currentNode)) break;
                        newNodes.add(currentNode);
                    }
                } while (!"".equals(currentNode.getPid()));
            }
        }
        return newNodes;
    }
    
Java可以通过使用if语句和逻辑运算符来实现多条件筛选查找。具体步骤如下: 1.定义一个数据结构来存储需要筛选的数据,比如一个包含多个属性的Java类。 2.定义多个筛选条件,每个条件都是一个布尔表达式,可以使用逻辑运算符将多个条件组合在一起。 3.遍历需要筛选的数据,对每个数据依次判断是否满足所有的筛选条件。 4.将符合条件的数据保存起来,可以使用集合等数据结构来存储。 示例代码如下: ``` public class Student { private String name; private int age; private double score; // 构造函数和getter/setter方法省略 } public class StudentFilter { public List<Student> filter(List<Student> students, int minAge, double minScore) { List<Student> result = new ArrayList<>(); for (Student student : students) { if (student.getAge() >= minAge && student.getScore() >= minScore) { result.add(student); } } return result; } } // 使用示例 List<Student> students = new ArrayList<>(); students.add(new Student("Tom", 20, 80.0)); students.add(new Student("Jerry", 18, 90.0)); students.add(new Student("Alice", 22, 85.0)); StudentFilter filter = new StudentFilter(); List<Student> result = filter.filter(students, 20, 85.0); ``` 上面的示例代码实现了一个学生筛选的功能,可以根据年龄和成绩来筛选符合条件的学生。其中,Student类表示学生信息,StudentFilter类实现了筛选逻辑。在filter方法中,使用了if语句和逻辑运算符来判断每个学生是否满足筛选条件,最后将符合条件的学生保存到一个集合中返回。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值