Take the 2-minute tour ×

I am trying to keep a temporary container of a class that contains member :

HashMap<Integer,myObject> myobjectHashMap

A class called myobjectsList

Then I do

myojbectsListA = new myojbectsList();
myojbectsListB = new myobjectsList();

then: Add some hashmap items to A (like2)


myobjectListB = myobjectListA; //B has 2

then: Add hashmap items to A; (like 4 more)

then return A to the items stored in B;

myobjectListA = myobjectListb;

but when I do this B grows with A while I am adding hashmap items to A. A now has 6 items in it because B had 6.

I want A to have original 2 still at end after last assingment in C++ i would use copy with objects, what is the java equivalent?

Added: OK I left something out explaining this.MyObjectsList does not contain the HashMap, it is derived from a class MyBaseOjbectsList which has the HashMap member and MyObjectsList extends MyBaseOjbectsList. Does this make a difference.?

share|improve this question
Can you post a SSCCE to give a better understanding of what you done so far? –  assylias Apr 9 '12 at 20:22
Your objects should implement the Cloneable interface, otherwise assignments such as MyObjectB = MyObjectA simply tell the JVM that both variables point to the same location in memory. Not two distinct objects. –  Mike McMahon Apr 9 '12 at 20:30
Btw, the overwhelming idiom (practically a law) is to capitalize class names. It will make your examples much more readable to those of us who use those things as quick cues while scanning example code. –  Kevin Welker Apr 9 '12 at 20:38
to add on @KevinWelker it also helps the syntax highlighter highlight the class names –  ratchet freak Apr 9 '12 at 20:54
@Mike - Thanks but tried to do cloneable, it made them both grow when one grew. I don't want that. –  user691305 Apr 10 '12 at 12:35
show 2 more comments

5 Answers

if you want a copy of the HashMap you need to construct a new one with

myobjectListB = new HashMap<Integer,myObject>(myobjectListA);

this will create a (shallow) copy of the map

share|improve this answer
or if it is already created, use myObjectListB.addAll(myObjectListA) –  Kevin Welker Apr 9 '12 at 20:36
Maybe i'm not understanding this but I don't need a copy of the map. I need a copy of the class that holds the map. ? There for myObjectListB has to be a class derived from MyojbectsList not a hashmap. –  user691305 Apr 10 '12 at 12:59
Also I don't understand the implementation of "addAll(myOjbejctListA)" –  user691305 Apr 10 '12 at 13:32
add comment

You can also use


Method to copy all elements from one hashmap to another hashmap

Program for copy all elements from one hashmap to another

import java.util.HashMap;

public class CloneHashMap {    
     public static void main(String a[]) {    
        HashMap hashMap = new HashMap();    
        HashMap hashMap1 = new HashMap();    
        hashMap.put(1, "One");
        hashMap.put(2, "Two");
        hashMap.put(3, "Three");
        System.out.println("Original HashMap : " + hashMap);
        hashMap1 = (HashMap) hashMap.clone();
        System.out.println("Copied HashMap : " + hashMap1);    

source : http://www.tutorialdata.com/examples/java/collection-framework/hashmap/copy-all-elements-from-one-hashmap-to-another

share|improve this answer
You can, but you probably shouldn't because Cloneable is essentially broken, per Joshua Bloch's Effective Java #11. –  Visionary Software Solutions Jun 14 '13 at 4:18
also, note that clone() returns a shallow copy. –  Vinay Wadhwa Sep 16 '13 at 10:13
add comment

In Java, when you write:

Object objectA = new Object();
Object objectB = objectA;

objectA and objectB are the same and point to the same reference. Changing one will change the other. So if you change the state of objectA (not its reference) objectB will reflect that change too.

However, if you write:

objectA = new Object()

Then objectB is still pointing to the first object you created (original objectA) while objectA is now pointing to a new Object.

share|improve this answer
that explains the problem, whats the solution? Or is this a limitation of Java with no work around? –  user691305 Apr 10 '12 at 13:23
That's not a limitation, it is the way it works. If you want 2 different objects, you create 2 objects - if you only create one object, then all the variables that point to that object are equal. –  assylias Apr 10 '12 at 13:25
I suggest you create a new question with all the relevant code (ideally a SSCCE) and explain precisely what you are trying to achieve. –  assylias Apr 10 '12 at 13:26
creating a new object and wiping out the original values is not the goal. The goal is to keep the original values and use only them again later in a copy that received more new values. –  user691305 Apr 10 '12 at 14:55
Well it really depends on the object. If it does not provide a way to create a new copy object, then it is complicated because the object has not been designed to be copied (maybe on purpose - but you can still do it using reflection). If the object does implement that feature (see for example the answer with the copy constructor of HashMap) then you just use the provided method. –  assylias Apr 10 '12 at 15:13
show 1 more comment

The difference is that in C++ your object is on the stack, whereas in Java, your object is in the heap. If A and B are Objects, anytime in java you do

B = A

A and B point to the SAME object. So anything you do to A you do to B and vice versa

use new HashMap() if you want two different objects

And you can use Map.putAll(...) to copy data between two Maps

share|improve this answer
Is that the same as saying Java is all by reference and C++ is sometimes reference and sometimes value? But I don't want a HashMap, I wan't a duplicate copy of the class I can use as temp storage. –  user691305 Apr 10 '12 at 13:02
I don't have access to the HashMap in the base class. Again, I have to work with classes derived from the base. I cannot access the HashMap in it. –  user691305 Apr 10 '12 at 13:24
add comment

What you assign one object to another, all you're doing is copying the reference to the object, not the contents of it. What you need to do is take your object B and manually copy the contents of object A into it.

If you do this often, you might consider implementing a clone() method on the class that will create a new object of the same type, and copy all of it's contents into the new object.

share|improve this answer
Clone did the same thing it just seemed to pass the reference. –  user691305 Apr 10 '12 at 13:06
Also, when one increases its HashMap count, the copy does too, don't want that. I want idependent containers(in this case classes) of the same type. –  user691305 Apr 10 '12 at 13:34
add comment

Your Answer


