Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8168705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:36:53+00:00 2026-06-06T20:36:53+00:00

I have a hashmap which I want to copy for other use. But whenever

  • 0

I have a hashmap which I want to copy for other use. But whenever I copy it and reuse it, it also changes the original one.
Why is that?

    do {
            Map<Integer, Map<String, Object>> map1 = originalMap; 
            //at the second iteration originalMap is the same as map1 of the last iteration, 
            //eventhough the change was nog accepted;
            //do something with map1 (change value);
            if(change is accepted) {
               originalMap = map1;
            }
        } while(iteration < 10);

Thanks in advance

    public static <Integer,String, Schedule>Map<Integer, Map<String, Schedule>> deepCopy(Map<Integer, Map<String, Schedule>> original) {
    Map<Integer, Map<String, Schedule>> copy = new HashMap<Integer, Map<String, Schedule>>();

    for (Map.Entry<Integer, Map<String, Schedule>> entry : original.entrySet()) {
        copy.put(entry.getKey(), deepCopy2(entry.getValue()));
    }
    return copy;
}

public static <String, Schedule>Map<String, Schedule> deepCopy2(Map<String, Schedule> original) {
    Map<String, Schedule> copy = new HashMap<String, Schedule>();
    for (Map.Entry<String, Schedule> entry : original.entrySet()) {
        copy.put(entry.getKey(), entry.getValue());
    }

    return copy;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:36:55+00:00Added an answer on June 6, 2026 at 8:36 pm

    What you did was not to create a copy of the map, but of the reference to it. when two references point to the same object, changes to one will reflect in the other.

    Solution 1: If this was a Map from some simple type to another, you would do this instead:

    Map<SomeType, OtherType> map1 = new HashMap<SomeType, OtherType>(original); 
    

    This is called a Copy Constructor. Almost All standard Collection and Map implementations have one, and it’s usually the simplest way to clone a simple structure.
    This will work fine as long as SomeType and OtherType are immutable (e.g. Integer and other Number types, Boolean, String, but not Collections, Dates, Maps, Arrays etc.)

    If not, as other answerers and commenters have pointed out, you also need to copy the map values.

    Solution 2: Here’s a quick and dirty version that should be safe:

    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()));
    }
    

    But actually, I like Hunter’s idea of providing a deep copy method. So here’s Solution 3:
    my own version using generic parameters:

    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;
    }
    

    You can call it like this:

    Map<Integer, Map<String, Object>> original=new HashMap<Integer, Map<String,Object>>();
    // do stuff here
    Map<Integer, Map<String, Object>> copy = deepCopy(original);
    

    Update

    I’ve hacked together a class that performs deep cloning for Maps, Collections and Arrays (primitive and otherwise). Usage:

    Something clone = DeepClone.deepClone(original);
    

    Here it is:

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which adds some arrays to a hashmap but then
I have a HashMap which I want to convert into a array. Which the
I have data stored in a HashMap, which I want to access via multiple
I have the following class: class DerivedMap extends Hashmap<String,Object> {} Which I use because
I have a HashMap, which contains another HashMap. I want to iterate over the
I have one grid view which i want to add in footer of listview
i want to clone a HashMap which have a Object as value. How does
I have a class which I want to set up as keys in HashMap.
In class 1 I have a hashmap which I send to my CustomAdapter. map.put(year,
I have HashMap 1, which contains 5 keys, all of which have Hashmaps as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.