1

I have a for each loop iterating in a list. I add a object to another Object list then i modify the value of the object then I add to another list.

Problem is first list value also changed after modifying the object before adding to the second list.

I am facing this type of reference/memory issue all over the project. In previous, i faced memory/reference issue even if i use new Keyword / addAll() etc method to copy list data.

orderListNormal = new HashMap<>();
orderListDelete = new HashMap<>();
orderListExtra = new HashMap<>();

for (Integer hashcode :
                salesOrderList.keySet()) {

            OrderSalesModel order = salesOrderList.get(hashcode);

            if (order.getQuantityAvailable() == 0) {
                if (isDirectOrder) {
                    processOrderListener.invalidQuantity();
                    return;
                }
                double regular = order.getQuantity();
                double extra = order.getQuantity() - order.getQuantityAvailable();
                regular = regular - extra;

                order.setQuantity(regular);
                orderListNormal.remove(hashcode);
                orderListDelete.put(hashcode, order);

                order.setOrderDetailID("0");
                order.setQuantity(extra);
                orderListExtra.put(hashcode, order);

            }
    ......
    }
Sadiq Md Asif
  • 882
  • 6
  • 18
  • 3
    Very very unclear: if it is a **list**, why can you invoke `keySet()` to then `get()` using a hashcode? Sorry, but we need a **real** [mcve] here. You are just posting parts of the relevant code. My guess is: you are **not** always "getting" different objects. But nobody can say for sure, given your insufficient input. – GhostCat Oct 24 '17 at 11:22
  • In that sense: look into your naming: you can all instance of a "model" class an "order"; and you call something a "list" ... that isn't a list?! – GhostCat Oct 24 '17 at 11:25
  • updated header to HashMap. – Sadiq Md Asif Oct 24 '17 at 11:27
  • 1
    Again, provide a [mcve]. Knowing "it is a map" isn't sufficient. Seriously: please **study** that link I give to you, and then improve your question accordingly. – GhostCat Oct 24 '17 at 11:28

2 Answers2

1

You should create a new OrderSalesModel if you don't want to mutate it. Since you are holding the reference by getting the OrderSalesModel with salesOrderList.get(hashcode) you will always mutate it. Make a copy constuctor and use it like this

OrderSalesModel order = new OrderSalesModel(salesOrderList.get(hashcode));
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • isn't there any way in java to copy, modify copied object expect copy constructor? How to solve the same problem for copying a list to another? – Sadiq Md Asif Oct 24 '17 at 11:26
  • Rather a guess, right? How do you know that `OrderSalesModel` actually has a constructor that takes an instance of said class? – GhostCat Oct 24 '17 at 11:26
  • @SadiqMdAsif It very much depends on what exactly you intend to do. It seems that you are lacking very basic knowledge about references and objects in java in general. You should rather study such basics before going for complex android tasks ... – GhostCat Oct 24 '17 at 11:27
  • @SadiqMdAsif I'm sorry, I did not understand what you exactly wrote. But you might want to read about [Java reference](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) and about [Copy Constructor / Cloning](https://stackoverflow.com/questions/2427883/clone-vs-copy-constructor-which-is-recommended-in-java) – Murat Karagöz Oct 24 '17 at 11:29
  • Object a = new Object(); Object b = new Object(a); Object b = a.clone(); is there any other way to copy avoiding referrence? I have tons of classes. It's not feasible to add copy constructor for all. – Sadiq Md Asif Oct 24 '17 at 11:33
1

You must clone your object. When you do:

A a = new A();
A b = a;

In fact both a and b has the same adresse when you change a, b will be changed. You must use Clone to duplicate and object in Java Take a look at this Tuto.

Fakher
  • 2,098
  • 3
  • 29
  • 45
  • "You **must** use clone". No, you *must* not. It is totally unclear what exactly the OP is doing ... and your recommendation pointing a single option is wrong, or at least very misleading. – GhostCat Oct 24 '17 at 11:25