hashmap简单实例(个人使用经验)
一、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 ="\t";
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()+"\n");
// 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);
}
}
hashmap简单实例(个人使用经验)的更多相关文章
- resteasy简单实例
1.建一个maven web项目 新建一个maven项目,next,第一个框不要勾选 选择maven-archetype-webapp,建一个web项目 键入项目组织id与项目id 一般此时搭建的只是 ...
- jsonrpc环境搭建和简单实例
一.环境准备 下载需要的jar包和js文件,下载地址:https://yunpan.cn/cxvbm9DhK9tDq 访问密码 6a50 二.新建一个web工程,jsonrpc-1.0.jar复制到 ...
- Hibernate(二)__简单实例入门
首先我们进一步理解什么是对象关系映射模型? 它将对数据库中数据的处理转化为对对象的处理.如下图所示: 入门简单实例: hiberante 可以用在 j2se 项目,也可以用在 j2ee (web项目中 ...
- 最新 Eclipse IDE下的Spring框架配置及简单实例
前段时间开始着手学习Spring框架,又是买书又是看视频找教程的,可是鲜有介绍如何配置Spring+Eclipse的方法,现在将我的成功经验分享给大家. 本文的一些源代码来源于码农教程:http:// ...
- 修改js confirm alert 提示框文字的简单实例
修改js confirm alert 提示框文字的简单实例: <!DOCTYPE html> <html> <head lang="en"> & ...
- 利用navicat创建存储过程、触发器和使用游标的简单实例
利用navicat创建存储过程.触发器和使用游标的简单实例 标签: navicat存储过程触发器mysql游标 2013-08-03 21:34 15516人阅读 评论(1) 收藏 举报 分类: 数 ...
- 【转】Android Https服务器端和客户端简单实例
转载地址:http://blog.csdn.net/gf771115/article/details/7827233 AndroidHttps服务器端和客户端简单实例 工具介绍 Eclipse3.7 ...
- Centos7的安装、Docker1.12.3的安装,以及Docker Swarm集群的简单实例
目录 [TOC] 1.环境准备 本文中的案例会有四台机器,他们的Host和IP地址如下 c1 -> 10.0.0.31 c2 -> 10.0.0.32 c3 -> 10.0.0. ...
- vue路由的简单实例
vue2.0 和 vue1.0 路由的语法还是有点稍微的差别,下面介绍一下vue-router 2的简单实例: <!DOCTYPE html> <html lang="en ...
随机推荐
- HTTPS介绍
超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了Web浏览器和网站服务器之间的传输报文,就可以直接读懂 ...
- JS文件中获取contextPath的方法
function getContextPath() { var pathName = document.location.pathname; var index = pathName.su ...
- 利用Express和ejs编写简单页面
1.创建临时文件夹ejsdemo $ mkdir ejsdemo 2.进入ejsdemo 初始化项目 $ npm init 3.安装express $ npm install express --sa ...
- Linux下查看alert日志文件的两种方法
--linux下查看alert日志文件的两种方法: --方法1: SQL> show parameter background_dump_dest; NAME TYPE VALUE ------ ...
- 【java集合系列】---HashSet
在前面的博文中,小编主要简单介绍了java集合中的总体框架,以及list接口中典型的集合ArrayList和LinkedList,接着,我们来看set的部分集合,set集合和数学意义上的集合没有差别, ...
- 改善database schema
本文地址:http://blog.csdn.net/sushengmiyan/article/details/50422102 本文作者:苏生米沿 Hibernate 读取你java模型类的映射元数据 ...
- java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener问题解决
今天搭建SSH项目的时候出现了如下错误: 严重: Error configuring application listener of class org.springframework.web.con ...
- android launcher 之踩到的坑
需求: 1. 用android系统launcher 隐藏主菜单 所有应用显示在桌面 即workspace上: 2.隐藏launcher上方默认的google search: 3.切换一套launche ...
- RxJava操作符(09-算术/聚合操作&连接操作)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51692493 本文出自:[openXu的博客] 目录: 算术聚合 Count Concat ...
- Android下DrawerLayout的使用
Android下DrawerLayout的使用 DrawerLayout见名知意,就是一个具有抽屉效果的布局,看看这个效果图,是不是感觉很炫酷 这么炫的效果其实不一定非要用类似一些SlidingMen ...