0

I feel I'm missing something basic in references VS values.In the following code, getRecentProduct is returning a reference to the list.

   public class ProductMapWrapper { 
    Map<String, List<ProductInformation>> productMap = new  HashMap<String, List<ProductInformation>>();

  public void putObject(ProductInformation productInfo, String key){
    List<ProductInformation> productInfoList = productMap.get(key);
    if (null == productInfoList)
        productInfoList = new ArrayList<ProductInformation>();
    productInfoList.add(productInfo);
    productMap.put(key, productInfoList);   
   }

  public ProductInformation getRecentProduct(String key){
    List<ProductInformation> productInfoList = productMap.get(key);
     productInfoList.get(0); //returns reference
    // the following is also returning reference
    List<ProductInformation> productinfoListCopy =  new ArrayList<ProductInformation>(productInfoList);
    return productinfoListCopy.get(0);
    }   
}
    // main function 
    ProductInformation productInfo = new ProductInformation();
    productInfo.setProdID("2323");
    ProductMapWrapper mapWrapper = new ProductMapWrapper();
    mapWrapper.putObject(productInfo, "MEDICAL");
    ProductInformation getObj =  mapWrapper.getRecentProduct("MEDICAL");
    System.out.println(getObj.getProdID());
    ProductInformation getObj1 =  mapWrapper.getRecentProduct("MEDICAL");
    getObj1.setProdID("test");
    System.out.println(getObj.getProdID()); // prints test

I followed different SO answers and mostly it has been suggested to use the following, but this is also returning reference.

  List<ProductInformation> productinfoListCopy =  new ArrayList<ProductInformation>(productInfoList);
    return productinfoListCopy.get(0);

Clone is working for me. But I wanted to know where I'm missing. Can someone help?

prakashb
  • 160
  • 1
  • 8
  • 2
    It doesn't return a reference to the list. It returns a reference to the first ProductInformation object of the list (and creates a copy of the list for no reason). What are you trying to achieve? What's the problem? – JB Nizet May 08 '15 at 22:30
  • @JB Nizet That's not what I intended to do. I want to do this productInfoList.get(0); So the reference to the object means, I cannot get copy of the productList's zero'th object without clone, am I right? – prakashb May 08 '15 at 22:34
  • 1
    You can't get a "cannot get copy of the productList" from a method whose return type is ProductInformation. If you want the method to return a List, its return type should be List. I still don't understand what you want. – JB Nizet May 08 '15 at 22:38
  • Sorry I corrected. What I want is the copy of the 0'th object. It works for me when I get the object and clone it. But I feel there is something I'm missing in design that can be achieved without a clone. – prakashb May 08 '15 at 22:39
  • 1
    If you want a copy, you'll need to create a copy. Using clone() or, preferrably, using a copy constructor. Creating a copy of the list to create a copy of its first element is useless (and won't even create a copy of its first element). The question is: why do you want a copy? – JB Nizet May 08 '15 at 22:43
  • Thank you. I got it. The class doesn't have copy constructor, and I cannot change it now. So I left with no option but Clone();.//The question is: why do you want a copy?// I have two types of productInformation, original and updated. I don't want original product informations changes while copying into updated ones. – prakashb May 08 '15 at 22:46

1 Answers1

2

The code you're using creates a copy of the list, but it's a "shallow copy". That means that it's a different list, but it's still referencing the same objects. And so if you get the first element of either list, you're getting a reference to the same object.

What you're trying to achieve is a "deep copy". You'll find a lot of information on the topic out there - here's an example question - it deals with arrays rather than lists but it's the same principle, and hopefully it's some useful reading Deep copy of an object array

Community
  • 1
  • 1
CupawnTae
  • 14,192
  • 3
  • 29
  • 60