利用HashMap存取对象并获得键值集合
1.HashMap 已实现的接口
Serializable, Cloneable, Map<K,V>
2.方法摘要
相关代码
/**
*
* @param ha
* write(HashMap<String,Customer> ha) 传来HashMap对象列表 将对象写入文件中
*/
public static void write(HashMap<String, Customer> ha) {
;
try {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("e:/db/db.dat"));
oos.writeObject(ha);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* @param id
* @return ReadCustomer(String id) 方法 通过HashMap 关键字 读取对象 并且返回对象
*/
public static Customer readCustomer(String mark) {
HashMap<String, Customer> hm = new HashMap<String, Customer>();
Customer c = new Customer();
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"e:/db/db.dat"));
hm = (HashMap<String, Customer>) ois.readObject();
c = hm.get(mark);
ois.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return c;
}
/**
* writeCustomer(Customer cus) 将对象写入HashMap 列表中并通过write()方法将对象写入文件中
*
* @param account
*/
public static void writeCustomer(Customer cus) {
HashMap<String, Customer> m = new HashMap<String, Customer>(); try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"e:db/db.dat"));
m = (HashMap<String, Customer>) ois.readObject();
m.put(cus.getMark(), cus);
write(m);
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Util.stay();
}
利用HashMap存取对象并获得键值集合的更多相关文章
- Hashtable键值集合
//Hashtable键值集合 键必须是维一的 类似于索引 Hashtable ht = new Hashtable(); ht.Add(, "中国"); ht.Add(, ); ...
- 键值集合List转换成datatable
/// <summary> /// 键值集合List转换成datatable /// </summary> /// <param name="data" ...
- 利用GeneratedKeyHolder获得新增数据主键值
Spring利用GeneratedKeyHolder,提供了一个可以返回新增记录所对应的主键值的方法: int update(PreparedStatementCreator psc, KeyHold ...
- hashmap可以用null为键值
import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class TestMain { ...
- JArray数组每个JObject对象添加一个键值对
JArray ja = new JArray(); JObject jo = new JObject(); jo.Add("1","1"); ja.Add(jo ...
- mybatis插入值的时候返回对象的主键值
mapping文件: <insert id="insert" parameterType="com.vimtech.bms.business.riskproject ...
- 将字符串转换成C#认可的对象(有键值对的对象)
var resobj = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JArray>(result ...
- Android(java)学习笔记105:Map集合的遍历之键值对对象找键和值
package cn.itcast_01; import java.util.HashMap; import java.util.Map; import java.util.Set; /* * Map ...
- Java基础知识强化之集合框架笔记53:Map集合之Map集合的遍历 键值对对象找键和值
1. Map集合的遍历(键值对对象找键和值) Map -- 夫妻对 思路: A: 获取所有结婚证的集合 B: 遍历结婚证的集合,得到每一个结婚证 C: 根据结婚证获取丈夫和妻子 转换: A: ...
随机推荐
- mysql获取行号的方法
1.不排序 语句: ) ) ) b,bigquestion 结果: 2.排序的 语句 ) ) ) b,bigquestion order by bigquestion.bigQuestionSequ ...
- 基于sentry的前端错误监控日志系统(部署sentry服务器/前端项目部署)-让前端最快的定位到生产问题
背景 在这越来越发达的网络时代,web应用也是越来越复杂,尤其是前端的开发,也是越来越受重视. 所以在我们前端开发完成后,会有一些列的web应用的上线验证,如自测.QA测试.code review 等 ...
- Linux进程IPC浅析[进程间通信SystemV共享内存]
Linux进程IPC浅析[进程间通信SystemV共享内存] 共享内存概念,概述 共享内存的相关函数 共享内存概念,概述: 共享内存区域是被多个进程共享的一部分物理内存 多个进程都可把该共享内存映射到 ...
- Exchanger使用
Exchanger使用
- input 文本框禁止输入表情
js在用户输入表情时自动过滤掉 <input type="text" id="input" maxlength="10"/> v ...
- Dom对象的经常用法
Dom对象的经常用法: (1)getElementById() 查询给定ID属性值的元素,返回该元素的元素节点 1. 查询给定ID属性值的元素,返回该元素的元素节点.也称为元素对象. ...
- Visual Studio VS如何重置所有设置
工具-导入和导出设置-重置所有设置,点击下一步即可.
- 开源 java CMS - FreeCMS2.2 敏感词管理
项目地址:http://www.freeteam.cn/ 敏感词管理 管理敏感词.系统会自己主动将敏感词替换为指定字符. 系统进行敏感词处理的功能有: 信息:标题.内容,摘要. 栏目:名称,描写叙述. ...
- 如何更有效的消灭watchdogs挖矿病毒?华为云DCS Redis为您支招
漏洞概述 近日,互联网出现watchdogs挖矿病毒,攻击者可以利用Redis未授权访问漏洞入侵服务器,通过内外网扫描感染更多机器.被感染的主机出现 crontab 任务异常.系统文件被删除.CPU ...
- shuf乱序排列
如何从文件中随即选取1000行内容呢?我们可以使用shuf命令 shuf [ 其中,shuf会读取file中的内容,并生成乱序的排列,进而使用head命令,取出前1000行