如何完全克隆一个HashMap包括 key 和 value。
我尝试枚举出里面的所有value,但Objdect类还是无法克隆。哪位有办法?
我尝试枚举出里面的所有value,但Objdect类还是无法克隆。哪位有办法?
解决方案 »
- 引用类型
- 关于java.io.IOException: CreateProcess: compress 1.tar error=2错误
- 一个字符串操作问题
- swing是不是你目前所见在工业界最优雅和最灵活的GUI框架呢?
- 100分求jcreater pro 3.00.093的注册码?
- 请问中jtable如何设成只读,不让用户修改。
- 求神仙解释while里面的内容第一次i= 0后查找index==-1,就break了,就跳出while了,i怎么可能还有机会再累加呢
- 菜鸟高分请教:怎样学java?
- 明天要去软脑面试,大家有什么建议?(急!在线等!)
- java新手求助,用jdk编程怎么查帮助啊?
- 选择.net还是Java,我的迷茫!!!进者有分
- java web start对客户段的jre版本有要求吗?
应该是浅拷贝
public class CloneMap { public static void main(String[] args) {
HashMap sourceMap=new HashMap();
HashMap destMap=null;
sourceMap.put("key1","value1");
sourceMap.put("key2","value2");
destMap=(HashMap)sourceMap.clone();
System.out.println("sourceMap: "+sourceMap);
System.out.println("destMap:"+destMap);
}
}
应该是浅拷贝
-----
他的意思是可以复制key和Object但是全是引用,如果你修改其中的的值也会影响原来的Hashmap里面的值的
暂时还没找到好办法实现。
暂时还没找到好办法实现。
static List unmodifiableList(List list)
Returns an unmodifiable view of the specified list.
static Map unmodifiableMap(Map m)
Returns an unmodifiable view of the specified map.
static Set unmodifiableSet(Set s)
Returns an unmodifiable view of the specified set.
是一点用处都没有的,只是map这个引用不能变而已,对数据没有任何约束
的方法可行,返回不可修改的试图,但是作者不要需要一份深拷贝吗?
的方法是什么意思呀?怎么看不太明白?
那么现在经过处理一下就是不可变的Map了
Map newMap=Collections.unmodifiableMap(oldMap)
然后将这个类实现Cloneable接口
然后在clone()方法里面实现深克隆
利用反射应该可以找到Object的类型吧。
怎么不行,
HashMap mapOnlyRead = (HashMap) Collections.unmodifiableMap(map);
回报错!
利用反射应该可以找到Object的类型吧。
你不能确定类型??怎么clone呢?
HashMap mapOnlyRead = (HashMap) Collections.unmodifiableMap(map);
at testHashMap.TestHashMap.main(TestHashMap.java:45)
就直接拷贝你最开始那两句代码运行都会报错的。而且我把代码改成这样,仍然可以修改呀:
Map map = new HashMap();
map.put("date", new Date());
Map mapOnlyRead = Collections.unmodifiableMap(map);
Date date = (Date) mapOnlyRead.get("date");
date.setYear(0);
System.out.println(mapOnlyRead.get("date"));
我从来不写这样的程序:
HashMap map=new HashMap();
我觉得这样很别扭,很不专业
任何时候都是
Map map=new HashMap();
你就自己写一个方法实现clone
public Map deepCloneMap(map src){
Map des=new HashMap();
for(Iterator it=src.keySet().iterator();it.hasNext();){
Object key=it.next();
Object value=src.get(key);
des.put(key.clone(),value.clone());
}
}
现在的问题就是从map里得到的是一个Object,但Object不能直接用clone方法呀!fog628(无名):
是呀,就是这个问题不能解决,所以才想用clone的。
就用序列化和反序列化否则你就定义一个接口来实现你的深拷贝
inteface DeepCloneable
import java.util.*;class CloneHashMap{
//深Clone Keys
private static ArrayList cloneKeys(Set set){
ArrayList oldList = new ArrayList(set);
ArrayList newList = (ArrayList)oldList.clone();
for (int i = 0; i< oldList.size() ; i++){
//Clone到每一个Key
newList.add(((Key)oldList.get(i)).clone());
}
return newList;
}
public static Map cloneMap(HashMap oldMap){
Map newMap = (HashMap)oldMap.clone();
ArrayList newKeys = cloneKeys(oldMap.keySet());
for (int i = 0; i < newKeys.size(); i++){
Object k = newKeys.get(i);
//Clone到每一个Value
Object v = ((Value)oldMap.get(k)).clone();
newMap.put(k, v);
}
return newMap;
}
public static void main(String[] args) throws Exception{
HashMap oldmap = new HashMap();
for (int i = 0; i < 5; i++){
oldmap.put(new Key(i), new Value(i));
}
System.out.println("更改值以前的HashMap:");
System.out.println(oldmap);
System.out.println("-----------------------------------");
System.out.println("Clone出来的HashMap:");
Map newmap = cloneMap(oldmap);
System.out.println(newmap);
System.out.println("-----------------------------------");
Iterator i = newmap.keySet().iterator();
while(i.hasNext()){
((Value)newmap.get(i.next())).increment();
}
System.out.println("更改Clone出来的HashMap中的值, 原来的旧HashMap");
System.out.println(oldmap);
System.out.println("-----------------------------------");
System.out.println("更改Clone出来的HashMap中的值, 现在的新的HashMap");
System.out.println(newmap);
}
}//一个用来作Value的类
class Value implements Cloneable{
private int value;
Value(int i){
this.value = i;
}
public void increment(){
value *= 100;
}
public Object clone(){
Object o = null;
try{
o = super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return o;
}
public String toString(){
return "" + value;
}
}
//一个用来作Key的类
class Key implements Cloneable{
private int value;
public Key(int i){
this.value = i;
}
public Object clone(){
Object o = null;
try{
o = super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return o;
}
public boolean equals(Object o){
return (o instanceof Key)
&&(this.value == ((Key)o).value);
}
public int hashCode(){
return this.value;
}
}
des.put( ((Cloneable) key).clone(),((Cloneable)value).clone());就可以了
如果需要一个通用的深拷贝,可以用序列化,但是你不要觉得效率低下!我也不忍心用这种方式
来实现的,幸好大部分情况是不需要深拷贝的.
public static Object clone(Object src) throws IOException, ClassNotFoundException{
if(src==null) return src;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(baos);
oos.writeObject(src);
ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois=new ObjectInputStream(bais);
return ois.readObject();
}
des.put( ((Cloneable) key).clone(),((Cloneable)value).clone());
好像也不行吧…………
clone方法确实在Object中定义的
怎么老不上QQ了/???????当然序列化是最容易了,但。。
又新发一贴,请进入看看:
http://community.csdn.net/Expert/topic/4062/4062251.xml?temp=.1975214