利用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: ...
随机推荐
- BUPT2017 springtraining(16) #1 题解
https://vjudge.net/contest/162590 A: 不难发现,当L=R时输出L,当L<R时输出2. B: 贪心得配对.1和n配 2和n-1配,对与对直接只要花1个代价就可以 ...
- S5700&S5710 产品文档 : 配置
http://support.huawei.com/hdx/hdx.do?docid=SC0000699332&lang=zh&path=PBI1-C103367%2FPBI1-C10 ...
- 【.Net 学习系列】-- EF Core实践(Code First)
一.开发环境: vs2015, .Net Framework 4.6.1 二.解决方案: 新建一个控制台应用程序 添加引用:Microsoft.EntityFrameworkCore.SqlServe ...
- Deepin-添加path
以管理员权限添加path(Debian系列) sudo gedit /etc/profile 添加path路径格式是: export PATH=”$PATH:your path1:your path2 ...
- Malformed or corrupted AST file: 'Unable to load module "...
Malformed or corrupted AST file: 'Unable to load module "/Users/topbar/Library/Developer/Xcode/ ...
- C#调用国家气象局天气预报接口
原文:C#调用国家气象局天气预报接口 一.需求 最近,刚好项目中有天气预报查询功能的需求,要求录入城市名称,获取该城市今日天气信息及相关气象生活辅助信息等. 例如:查询北京市天气 结果为: 今日北京天 ...
- vs2010下配置CUDA出现kernel launch failed问题,内核无效
首先, 推荐一篇不错的配置文档~手把手教你 CUDA 5.5与VS2010编译环境的搭建.笔者就是在这篇文章的指导下成功地在VS2010上搭建了CUDA 6.5~ 其次. 文末给出的执行演示样例不好使 ...
- STM32通过调用库函数进行编程
1.调用库函数编程和直接配置寄存器编程的差别: 2.CMSIS标准: 3.STM32库函数的组织: 4.程序例举: 调用库函数实现通过USART发送数据(26个大写的英文字母) 首先:在主函数部分先要 ...
- 怎样在 AutoLayout 中使用 UIScrollView (多个ContentView)
http://codehappily.wordpress.com/2013/11/14/ios-how-to-use-uiscrollview-with-autolayout-pure-autolay ...
- mac for smartSVN9 (8,9)破解方法 附smartSvn_keygen工具图文
mac for smartSVN9 (8,9)破解方法 附smartSvn_keygen工具 工具文件下载: http://files.cnblogs.com/files/xueshanshan/s ...