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

来源:互联网 发布:淘宝天猫内部优惠券群 编辑:程序博客网 时间:2024/05/23 16:08
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.csdn.net/20170531200556070?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFweWFuZHhweQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
原创粉丝点击