出现又离开亅 2023-06-27 15:39 采纳率: 42.9%
浏览 25

java实现省市区转换成树形结构

有一条sql记录。

INSERT INTO `school_info` ( `schoolCode`,   `name`, `schoolAlias`, `enName`, `address`, `provinceCode`, `cityCode`, `areaCode`, `cityName`) VALUES (10086, '测试学校', NULL, NULL, NULL, 330000, 330100, 330101,  'wz');

需求:提取出相关的省,市,区,学校的信息,并组成树状的数据结构。
类似于:

img

  • 写回答

3条回答 默认 最新

  • 瞬间的未来式 2023-06-27 15:48
    关注

    该回答引用chatgpt:
    先把所有查出来,然后组装成树
    参考一下

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    class Location {
        int code;
        String name;
        List<Location> children;
    
        Location(int code, String name) {
            this.code = code;
            this.name = name;
            this.children = new ArrayList<>();
        }
    }
    
    public class LocationTree {
        public static void main(String[] args) {
            List<Map<String, Object>> records = new ArrayList<>();
    
            // 假设从数据库中获取到了相关的省市区数据,存储在records列表中
    
            // 将记录转换为Location对象
            List<Location> locations = buildLocations(records);
    
            // 构建树状结构
            List<Location> tree = buildTree(locations);
    
            // 打印树形结构
            printTree(tree, 0);
        }
    
        private static List<Location> buildLocations(List<Map<String, Object>> records) {
            List<Location> locations = new ArrayList<>();
    
            for (Map<String, Object> record : records) {
                int code = (int) record.get("code");
                String name = (String) record.get("name");
    
                Location location = new Location(code, name);
                locations.add(location);
            }
    
            return locations;
        }
    
        private static List<Location> buildTree(List<Location> locations) {
            Map<Integer, Location> locationMap = new HashMap<>();
    
            // 将每个Location对象存储到map中,以code为键
            for (Location location : locations) {
                locationMap.put(location.code, location);
            }
    
            // 构建树状结构
            List<Location> tree = new ArrayList<>();
            for (Location location : locations) {
                int parentCode = location.code / 10000 * 10000;
                Location parent = locationMap.get(parentCode);
                if (parent != null) {
                    parent.children.add(location);
                } else {
                    tree.add(location); // 如果没有父节点,将其视为根节点
                }
            }
    
            return tree;
        }
    
        private static void printTree(List<Location> tree, int level) {
            for (Location location : tree) {
                for (int i = 0; i < level; i++) {
                    System.out.print("  "); // 根据层级缩进
                }
                System.out.println(location.name);
    
                printTree(location.children, level + 1); // 递归打印子节点
            }
        }
    }
    
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 6月27日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表