Java编程中的HashMap
如何将字符串到向量的映射复制到另一个映射。 我正在这样做Java编程中的HashMap,java,hashmap,copy,Java,Hashmap,Copy,如何将字符串到向量的映射复制到另一个映射。 我正在这样做 Map<String,Vector<allocated>>map_to_input_copy=new HashMap<String,Vector<allocated>>(); Map<String,Vector<allocated>>map_to_output_copy=new HashMap<String,Vector<allocated>>
Map<String,Vector<allocated>>map_to_input_copy=new HashMap<String,Vector<allocated>>();
Map<String,Vector<allocated>>map_to_output_copy=new HashMap<String,Vector<allocated>>();
map_to_input_copy.putAll(map_to_inputs);
map_to_output_copy.putAll(map_to_outputs);
及
这里的输出是分配类中的属性
但两者都不同。请帮我一个忙。您正在将矢量对象复制到新地图中,因此在新地图中有相同的矢量对象,具有相同的内容。如果修改向量对象的内容,它将显示在两个贴图中,因为它是相同的 您需要做的是所谓的深度复制,这意味着对条目进行迭代,并创建向量对象的副本 顺便说一句:不鼓励使用Vector类。ArrayList是现代且更快的替代品
System.out.println("before"+map_to_inputs.get("5c").get(1).output);
System.out.println("after"+map_to_inputs.get("5c").get(1).output);