日期:2014-05-20  浏览次数:20870 次

HashMap用法,急急!!!
是这样的:
public static HashMap<String, String> telMap = new HashMap<String, String>();
public static HashMap<String, String> loadPhone(String filename)throws IOException {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(
filename), "UTF-8"));
String str = null;
String key = null;
String value =null;
while ((str = br.readLine()) != null) {
String[] ss = str.split(" ", 2);
key = ss[0];
value = ss[1];

telMap.put(key, value);
}
br.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return telMap;
}



上面一个方法是用来把一文本1内的内容装到telMap 
这文本内的内容是:
11000000 北京市
11010000 市辖区
11010100 东城区
11010200 西城区
11010300 崇文区
类似这样的,前面的是城市区号编码,作为key
后面的是城市名称作为value


再有一个另外的文本2,里面的内容是
晋城
遵义
北京
柳州
海口
大理
德阳

想通过文本1给文本2的城市名都配上相应的城市编号
并写到文本3上,文本3的格式和文本1一样
文本2的城市名与文本1的城市名少了个市或县、区这种字,我知道如果是全字匹配,可以用下面的方法解决,但是现在少了个字,怎么解决啊

public void getPhoneAttribution(String cityname) {
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("cityregion.data",true));

if (telMap.containsValue(cityname)) {
bw.write("\n"+cityname+" ");
bw.write(telMap.get(cityname));
} else {
System.out.println("the city's region is null!");
}
bw.flush();
bw.close();
}catch(Exception e){
e.printStackTrace();
}


}

再用这个containsValue应该不行吧?

------解决方案--------------------
你试一下下面的代码行不行
Java code
public void getPhoneAttribution(String cityname) {
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("cityregion.data",true));
String cityName1=cityname+"区";
String cityName2=cityname+"市";
if (telMap.containsValue(cityName1)) {
bw.write("\n"+cityName1+" ");
bw.write(telMap.get(cityName1));
}else{
if (telMap.containsValue(cityName2)) {
bw.write("\n"+cityName2+" ");
bw.write(telMap.get(cityName2));
}else {
System.out.println("the city's region is null!");
}
} 

bw.flush();
bw.close();
}catch(Exception e){
e.printStackTrace();
}


}

------解决方案--------------------
Java code

        //讀出1.txt中的內容
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\1.txt"), "BIG5"));
        Map<String, String> fa = new HashMap<String, String>();
        String temp = null;
        while ((temp = br.readLine()) != null) {
            fa.put(temp.split(" ")[0], temp.split(" ")[1]);
        }
        br.close();

        //讀出2.txt中的內容與fa中的內容進行匹配
        BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\2.txt"), "BIG5"));
        Map<String, String> fa1 = new HashMap<String, String>();
        String temp1 = null;
        while ((temp1 = br1.readLine()) != null) {
            for (Iterator<Entry<String, String>> it = fa.entrySet().iterator(); it.hasNext();) {
                Entry<String, String> e = it.next();

                if (e.getValue().matches(".*" + temp1 + ".*")) {
                    fa1.put(e.getKey(), temp1);
                }
            }
        }
        br1.close();

        //寫到新文件
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\3.txt"), "BIG5"));
        for (Iterator<Entry<String, String>> it = fa1.entrySet().iterator(); it.hasNext();) {
            Entry<String, String> e = it.next();
            bw.write(e.getKey() + " " + e.getValue() + " \r\n");
        }
        bw.flush();
        bw.close();