瑾瑜i 2023-06-15 09:18 采纳率: 33.3%
浏览 42
已结题

java 获取集合中的父id

一个List单位对象集合,主要字段是id parentId(本单位的父单位id,有多级) UnionCode(父code是2位数,子级每级递增2位)需要查找出这个集合中所存在的最上级的单位,(假如有父级单位A,子级单位为A1,A1的子级单位A2,集合中存在A1不存在A,那么最上级单位是A1,)以及如果存在中间有截断的情况,(如:集合中存在A,A2,需要将A2的parentId修改为A的id)

  • 写回答

2条回答 默认 最新

  • 喝茶品人生 2023-06-15 10:04
    关注

    根据你的问题我第一反应用递归来做, 遍历集List合找到没有父单位的单位作为候选的最上级单位,然后递归地查找父级单位直到找到最顶层的单位

    class Unit {
        Integer id;
        Integer parentId;
        String unionCode;
    }
    
    public Unit findTopUnit(List<Unit> units) {
        List<Unit> candidates = new ArrayList<>();
        for (Unit unit : units) {
            if (unit.parentId == null || !containsUnit(units, unit.parentId)) {
                candidates.add(unit);
            }
        }
        if (candidates.isEmpty()) {
            return null;
        }
        Unit topUnit = candidates.get(0);
        for (Unit candidate : candidates) {
            if (isAncestor(units, candidate, topUnit)) {
                topUnit = candidate;
            }
        }
        return topUnit;
    }
    
    private boolean isAncestor(List<Unit> units, Unit ancestor, Unit unit) {
        if (ancestor.id.equals(unit.id)) {
            return true;
        }
        String parentUnionCode = getParentUnionCode(unit.unionCode);
        Integer parentId = getParentId(units, parentUnionCode);
        if (parentId == null) {
            Unit parentUnit = new Unit();
            parentUnit.id = -1;
            parentUnit.parentId = null;
            parentUnit.unionCode = parentUnionCode;
            units.add(parentUnit);
            parentId = parentUnit.id;
            unit.parentId = parentId;
        }
        unit.parentId = parentId;
        return isAncestor(units, ancestor, getUnitById(units, parentId));
    }
    
    private Integer getParentId(List<Unit> units, String unionCode) {
        for (Unit unit : units) {
            if (unit.unionCode.equals(unionCode)) {
                return unit.id;
            }
        }
        return null;
    }
    
    private String getParentUnionCode(String unionCode) {
        int length = unionCode.length();
        if (length <= 2) {
            return null;
        }
        return unionCode.substring(0, length - 2);
    }
    
    private boolean containsUnit(List<Unit> units, Integer id) {
        for (Unit unit : units) {
            if (unit.id.equals(id)) {
                return true;
            }
        }
        return false;
    }
    
    private Unit getUnitById(List<Unit> units, Integer id) {
        for (Unit unit : units) {
            if (unit.id.equals(id)) {
                return unit;
            }
        }
        return null;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月23日
  • 已采纳回答 6月15日
  • 创建了问题 6月15日

悬赏问题

  • ¥15 scottplot5
  • ¥15 想问问这个建模怎么编程没有思路
  • ¥15 关于imageENview(ImageEN)中新建图层并根据鼠标位置添加图标
  • ¥100 用两台电脑局域联网进行MT5的EA参数优化,但是连接不上
  • ¥15 FastAPI报错: AsyncSession不是有效Pydantic类型
  • ¥50 这Mac系统提示虚拟内存不足,怎么解决
  • ¥15 Rs232电路无法收发数据,求帮助
  • ¥15 百度cookie扫码登录器
  • ¥15 微机原理汇编语言debug调试实验
  • ¥23 matlab可以把相图转换为庞加莱映射吗