java + 省市区的数据结构划分

本文探讨如何使用Java来高效地组织和管理省市区的层级数据,包括数据结构的选择、数据存储和查询优化等方面,旨在提供一种适用于大型应用的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.company;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class Main {

    public static void main(String[] args) {


        String fileName = "/Users/dllo/Desktop/area.txt";
        String content = readFileByLines(fileName);

        System.out.println(content);

        String str[] = content.split("\n");

        System.out.println("666" + Arrays.toString(str));

        ArrayList<HashMap> proList = new ArrayList<HashMap>();

        for (int i = 0; i < str.length; i++) {
          //省
            if (!str[i].startsWith(" ")) {
                HashMap<Object, Object> proHashMap = new HashMap<>();
                proHashMap.put("proName", str[i]);
                ArrayList<HashMap> cityList = new ArrayList<HashMap>();
                proHashMap.put("cityList", cityList);
                proList.add(proHashMap);
            }
            //市
            if (str[i].startsWith("  ") && !str[i].startsWith("    ")) {
                HashMap<Object, Object> cityHashMap = new HashMap<>();
                cityHashMap.put("cityName", str[i]);
                ArrayList<String> areaList = new ArrayList<String>();
                cityHashMap.put("areaList", areaList);
                HashMap<Object, Object> proHashMap = new HashMap<>();
                proHashMap = (HashMap) proList.get(proList.size() - 1);
                ArrayList<HashMap> cityList = new ArrayList<>();
                cityList = (ArrayList) proHashMap.get("cityList");
                cityList.add(cityHashMap);
            }
            //区
            if (str[i].startsWith("    ")) {
                HashMap<Object, Object> proHashMap = new HashMap<>();
                proHashMap = (HashMap) proList.get(proList.size() - 1);
                ArrayList<HashMap<Object, Object>> cityList = new ArrayList<>();
                cityList = (ArrayList) proHashMap.get("cityList");
                HashMap<Object, Object> cityHashMap = new HashMap<>();
                cityHashMap = (HashMap) cityList.get(cityList.size() - 1);
                ArrayList<String> areaList = new ArrayList<>();
                areaList = (ArrayList) cityHashMap.get("areaList");
                areaList.add(str[i]);
            }
        }
        System.out.println("**~~*~~~~**" + proList);
    }
    public static String readFileByLines(String fileName) {
        String content = null;
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容,一次读一整行:");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                content = content + tempString + "\n";
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return content;
    }
}
![这里写图片描述](http://img.blog.youkuaiyun.com/20170531200556070?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFweWFuZHhweQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值