आप क्या धन्यवाद था नक्शे की एक प्रति बनाने के लिए नहीं है, लेकिन यह करने के लिए संदर्भ की। जब दो संदर्भ एक ही वस्तु को इंगित करते हैं, तो एक में परिवर्तन दूसरे में दिखाई देगा।
समाधान 1: यदि यह एक और के लिए कुछ सरल प्रकार से एक नक्शा था, तो आप इस बजाय करना होगा:
Map<SomeType, OtherType> map1 = new HashMap<SomeType, OtherType>(original);
यह एक Copy Constructor कहा जाता है। लगभग सभी मानक संग्रह और मानचित्र कार्यान्वयन में एक है, और यह आमतौर पर एक साधारण संरचना को क्लोन करने का सबसे आसान तरीका है। यह रूप में लंबे समय के रूप में SomeType
और OtherType
हैं immutable तरीके से कार्य करेंगे (Integer
जैसे और अन्य Number
प्रकार, Boolean
, String
, लेकिन नहीं संग्रह, तिथियाँ, मैप्स, सारणी, आदि)
यदि नहीं
, अन्य answerers और टिप्पणीकर्ताओं है के रूप में इंगित किया गया है, आपको मानचित्र मानों की प्रतिलिपि बनाने की भी आवश्यकता है।
समाधान 2: यहाँ एक त्वरित और गंदे संस्करण है कि सुरक्षित होना चाहिए है:
Map<Integer, Map<String, Object>> original=new HashMap<Integer, Map<String,Object>>();
Map<Integer, Map<String, Object>> copy =
new HashMap<Integer, Map<String, Object>>();
for(Entry<Integer, Map<String, Object>> entry : original.entrySet()){
copy.put(entry.getKey(), new HashMap<String, Object>(entry.getValue()));
}
लेकिन वास्तव में, मैं एक गहरी प्रतिलिपि विधि प्रदान करने का हंटर के विचार की तरह। तो यहाँ समाधान 3 है: सामान्य मानकों का प्रयोग करके अपने खुद के संस्करण:
public static <K1, K2, V> Map<K1, Map<K2, V>> deepCopy(
Map<K1, Map<K2, V>> original){
Map<K1, Map<K2, V>> copy = new HashMap<K1, Map<K2, V>>();
for(Entry<K1, Map<K2, V>> entry : original.entrySet()){
copy.put(entry.getKey(), new HashMap<K2, V>(entry.getValue()));
}
return copy;
}
आप इसे इस तरह कॉल कर सकते हैं:
Map<Integer, Map<String, Object>> original=new HashMap<Integer, Map<String,Object>>();
// do stuff here
Map<Integer, Map<String, Object>> copy = deepCopy(original);
अद्यतन
मैं एक साथ एक काट दिया गया है कक्षा जो मानचित्र, संग्रह और Arrays (आदिम और अन्यथा) के लिए गहरी क्लोनिंग करता है। उपयोग:
Something clone = DeepClone.deepClone(original);
संदेश यह है:
public final class DeepClone {
private DeepClone(){}
public static <X> X deepClone(final X input) {
if (input == null) {
return input;
} else if (input instanceof Map<?, ?>) {
return (X) deepCloneMap((Map<?, ?>) input);
} else if (input instanceof Collection<?>) {
return (X) deepCloneCollection((Collection<?>) input);
} else if (input instanceof Object[]) {
return (X) deepCloneObjectArray((Object[]) input);
} else if (input.getClass().isArray()) {
return (X) clonePrimitiveArray((Object) input);
}
return input;
}
private static Object clonePrimitiveArray(final Object input) {
final int length = Array.getLength(input);
final Object copy = Array.newInstance(input.getClass().getComponentType(), length);
// deep clone not necessary, primitives are immutable
System.arraycopy(input, 0, copy, 0, length);
return copy;
}
private static <E> E[] deepCloneObjectArray(final E[] input) {
final E[] clone = (E[]) Array.newInstance(input.getClass().getComponentType(), input.length);
for (int i = 0; i < input.length; i++) {
clone[i] = deepClone(input[i]);
}
return clone;
}
private static <E> Collection<E> deepCloneCollection(final Collection<E> input) {
Collection<E> clone;
// this is of course far from comprehensive. extend this as needed
if (input instanceof LinkedList<?>) {
clone = new LinkedList<E>();
} else if (input instanceof SortedSet<?>) {
clone = new TreeSet<E>();
} else if (input instanceof Set) {
clone = new HashSet<E>();
} else {
clone = new ArrayList<E>();
}
for (E item : input) {
clone.add(deepClone(item));
}
return clone;
}
private static <K, V> Map<K, V> deepCloneMap(final Map<K, V> map) {
Map<K, V> clone;
// this is of course far from comprehensive. extend this as needed
if (map instanceof LinkedHashMap<?, ?>) {
clone = new LinkedHashMap<K, V>();
} else if (map instanceof TreeMap<?, ?>) {
clone = new TreeMap<K, V>();
} else {
clone = new HashMap<K, V>();
}
for (Entry<K, V> entry : map.entrySet()) {
clone.put(deepClone(entry.getKey()), deepClone(entry.getValue()));
}
return clone;
}
}
क्योंकि यह 'हैश मैप' की एक प्रति नहीं है, यह वास्तविक 'हैश मैप' के लिए _reference_ है, जिसका अर्थ है कि किसी भी बदलाव से दूसरे को प्रभावित किया जाएगा। आपको 'हैश मैप' –