HashMap map = new HashMap();
map.put("province", "北京");
map.put("coding", "123");
HashMap map1 = new HashMap();
map1.put("province", "上海");
map1.put("coding", "456");
List<HashMap> maps = new ArrayList<HashMap>();
maps.add(map);
maps.add(map1);
File file = new File("province.out");
ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream(file));
Map map2 = new HashMap();
for (int i = 0; i < maps.size(); i++) {
for (HashMap mp : maps) {
String province = (String) mp.get("province");
String coding = (String) mp.get("coding");
map2.put(province, coding);
}
}
oout.writeObject(map2);
oout.close();
ObjectInputStream oin = new ObjectInputStream(new FileInputStream(file));
Map map3 = (Map) oin.readObject();
oin.close();
System.out.println("北京:"+map3.get("北京"));
System.out.println("上海:"+map3.get("上海"));
打印结果:
北京:123
上海:456
不建议使用jdk的序列化。性能问题。
可以用第三方