一、HashMap<int,String>是错误的:因为int是基本类型,而key和value要求是对象,所以要用Integer而不是int。HashMap<String,Object>的value一定要是Object类型。
二、HashMap<>同一元素添加二次覆盖
HashMap test = new HashMap<Integer, String>();
test.put(1, "i");
test.put(2, "you");
test.put(3, "she");
test.put(3, "he");
那么,test.size是3,而key为3所对应的value为he。
三、hashmap综合应用实例:
文件region_template基本地域数据,从geoip.txt的ip映射的地域数据中提取省、市信息,补充到region_template后面输出到新文件region_template_analytic。
代码如下:
importjava.io.*;
importjava.util.HashMap;
import java.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
import java.io.OutputStreamWriter;
importjava.util.ArrayList;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Set;
importjava.util.Vector;
importcom.vpon.wizad.etl.util.Region;
public classmappingToRegion_template {
private HashMap<String, Region>myRegionTemplates;
private HashMap<String, Region>RegionAnalytic;
public mappingToRegion_template(StringregionFile)
throwsUnsupportedEncodingException, FileNotFoundException {
this.myRegionTemplates = newHashMap<String, Region>();
this.RegionAnalytic = newHashMap<String, Region>();
File allRegion = newFile(regionFile);
BufferedReader br = null;
String line = "";
String cvsSplitBy =",";
if (allRegion == null)
System.out.println("nofile of" + regionFile);
else {
try {
br = newBufferedReader(new InputStreamReader(new FileInputStream(regionFile),"utf-8"));
while((line = br.readLine()) != null) {
//Skip empty lines.
if(line.matches("^[\n\s]+$")) {
continue;
}
String[]row = line.split(cvsSplitBy);
// System.out.println(line.toString());
StringregionID = row[0];
StringregionName = row[1];
booleancountry = false;
booleanprovince = false;
intcityLevel = 0;
if(row[2].equals("1")) {
country= true;
}
if(row[3].equals("1")) {
province= true;
}
try{
cityLevel= Integer.parseInt(row[4]);
}catch (Exception e) {
cityLevel= 0;
}
StringparentRegionID = row[5];
if(!parentRegionID.matches("\d+")) {
parentRegionID= null;
}
// if(regionName.matches("^包")){
// System.out.println(regionName);
//
// }
if(regionName.matches("(.*)(市|省)$")){//thesame as "(.*)[市省]$"
//if(regionName.endsWith("省")|regionName.endsWith("市")){
regionName= regionName.substring(0, regionName.length()-1);
}elseif( regionName.matches("(.*)(区)$") )
{
if(regionName.startsWith("内蒙古")){
regionName= regionName.substring(0,3);
}else{
regionName= regionName.substring(0,2);
}
// System.out.println(regionName);
}
Regionregion = new Region(regionID, regionName, country,province, cityLevel,parentRegionID);
// System.out.println(region.getName());
if(this.myRegionTemplates.containsKey(regionName))
System.out.println(regionName);
this.myRegionTemplates.put(regionName,region);
this.RegionAnalytic.put(regionID,region);
}
} catch (FileNotFoundExceptione) {
e.printStackTrace();
} catch (IOExceptione) {
e.printStackTrace();
} finally {
if (br !=null) {
try{
br.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 吉林处理后的问题,少了
System.out.println(this.myRegionTemplates.size());
System.out.println(this.RegionAnalytic.size());
}
private void mapADAssociation(StringgeoipFile) throws IOException
{
System.out.println("pid:"+this.myRegionTemplates.get("吉林").getRegionID());
int regionNumber =this.RegionAnalytic.size();
BufferedReader br = null;
String line = "";
String FilesplitBy =" ";
try{
br = newBufferedReader(new InputStreamReader(new FileInputStream(
geoipFile),"utf-8"));
while ((line =br.readLine()) != null) {
// Skipempty lines.
if(line.matches("^[\n\s]+$")) {
continue;
}
String[]row = line.split(FilesplitBy);
Stringgeoip_province = row[2];
Stringgeoip_city = row[3];
//city isnull: prosses as province
if(geoip_city == null) {
if(geoip_province == null) {
thrownew RuntimeException("province and city are both null");
}else{
geoip_city= geoip_province;
}
}
//System.out.println(geoip_province+","+geoip_city);
if(geoip_province.matches("国外"))
continue;
if(geoip_province == geoip_city) {
if(this.myRegionTemplates.containsKey(geoip_province)) {
continue;
}else {
System.out.println(geoip_province);
thrownew RuntimeException("illegal province: no that " + geoip_province);
}
} else {
if(this.myRegionTemplates.containsKey(geoip_city)){
continue;
}else{
// System.out.println(geoip_province+","+geoip_city);
RegionparentRegion = this.myRegionTemplates.get(geoip_province);
StringparentRegionID= parentRegion.getRegionID();
booleancountry = false;
booleanprovince = false;
int cityLevel = 0;
String regionID =Integer.toString(++regionNumber);
Region region = new Region(regionID,geoip_city, country, province, 0, parentRegionID);
// this.myRegionTemplates.put(geoip_city, region);
this.RegionAnalytic.put(regionID,region);
}
}
}
} catch (RuntimeException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
private void writeToFile(StringoutputFile) throws IOException {
File file = newFile(outputFile);
if(!file.exists())
file.createNewFile();
FileOutputStream out=newFileOutputStream(file,true);
System.out.println(this.myRegionTemplates.size());
System.out.println(this.RegionAnalytic.size());
for(int i = 1; i <=this.RegionAnalytic.size(); i++)
{
Region outRegion =this.RegionAnalytic.get(Integer.toString(i));
int isCountry = outRegion.isCountry()?1:0;
int isProvince =outRegion.isProvince()?1:0;
StringBuffer sb =new StringBuffer(outRegion.getRegionID()+","+outRegion.getName()
+","+isCountry+","+isProvince+","+outRegion.getCityLevel()+","+outRegion.getParentRegionID()+" ");
// StringBuffer sb=newStringBuffer(outRegion.toString());
out.write(sb.toString().getBytes("utf-8"));
}
}
public static void main(String[] args)throws Exception {
String regionFile = "F:/region_template/region_template";
String geoipFile ="F:/geoip.txt";
String outputFile ="F:/region_template_analytic/region_template_analytic";
mappingToRegion_template db =new mappingToRegion_template(regionFile);
db.mapADAssociation(geoipFile);
db.writeToFile(outputFile);
}
}