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 ...
随机推荐
- 修改SQL数据库中表字段类型时,报“一个或多个对象访问此列”错误的解决方法
在SQL数据库中使用SQL语句(格式:alter table [tablename] alter column [colname] [newDataType])修改某表的字段类型时,报一下错误:由于一 ...
- Machine Learning From Scratch-从头开始机器学习
Python implementations of some of the fundamental Machine Learning models and algorithms from scratc ...
- Architecture : Describable Command and Identifiable Data
Architecture : Describable Command and Identifiable Data Description Terms Command A command is a fu ...
- easyui datagrid 横向滚动条
要出现横向滚动条则不能有fitColumns:true/false
- 荣耀10带来AI版WPS,玩转潮酷新功能
图书馆里,想把喜欢的句子和情节留存, 无奈摘抄需要时间,拍下来又很容易遗忘在相册? 课堂偷偷拍摄的课件, 模糊一片难以辨认? 开会培训收集的PPT照片, 总有那么几页对焦失败? 这些当时起劲,后来就& ...
- 干货满满,腾讯云+社区技术沙龙 Kafka Meetup 深圳站圆满结束
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 云+导语:4月22日,由腾讯云和 Kafka 社区主办.开源中国协办的腾讯云+社区技术沙龙 Kafka Meetup 深圳站在腾讯大厦举行, ...
- Mysql B-Tree, B+Tree, B*树介绍
[摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tree索引,InnoDB还支持B+Tree索引,Memory ...
- SUSE10的虚拟机安装以及ORACLE 11g的安装
SUSE10虚拟机安装与ORACLE安装 作者:张欣橙 本文所需要的所有参数均位于文末附录中 一.SUSE10虚拟机的安装与创建 新建虚拟机安装 选择下一步 选择下一步 选择下一步 选择下一步 选择下 ...
- iOS多线程编程--NSOperation(转)
这篇文章写得非常不错,基础用法都涉及到了,我把文章提到的例子都写到了demo里面, 原文地址: iOS多线程--彻底学会多线程之『NSOperation』 demo下载:https://github. ...
- 代理delegate
1>代理的用处是什么? 监听那些不能通过addTarget监听的事件 主要用开负责在两个对象之间,发生某些事件时,传递或发送消息 当我们需要 监听某些事件时,但苹果没有提供相关监听方法(addt ...