HashMap用法,急急!!!

luhy5499 2011-09-20 03:39:32
是这样的:
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应该不行吧?
...全文
144 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mhshran5408 2011-09-20
  • 打赏
  • 举报
回复
呵呵 学习了
qqhw123 2011-09-20
  • 打赏
  • 举报
回复
直接把你之前的那个value 当做是新的HashMap的key应该就可以了啊?
这样也关联起来了啊
小绵羊 2011-09-20
  • 打赏
  • 举报
回复
我的系統是繁體的所以用了BIG5編碼,lz自己改回去就可以了

那個正則不是很嚴格,如果碰到一個地名包含另外一個地名就有點不靈了。

那個正則貌似把前面的 .* 去掉會好一些
小绵羊 2011-09-20
  • 打赏
  • 举报
回复

//讀出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();
风尘中国 2011-09-20
  • 打赏
  • 举报
回复
你试一下下面的代码行不行
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();
}


}
luhy5499 2011-09-20
  • 打赏
  • 举报
回复
cityname是文本2的内容,难办了,不会写了,那位大哥帮心写一下啊
小绵羊 2011-09-20
  • 打赏
  • 举报
回复
正則表達式
风尘中国 2011-09-20
  • 打赏
  • 举报
回复
你把cityname截取前两个字符 作为HashMap的键
要么你就从文件里面读取的区名或者城市名称 字符串 再添加一个"市"或者"区"进行匹配

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧