Below is some Java code that I am having a problem with:import java.util.HashMap;import java.util.ArrayList;public class MyTestClass {  public static void main(String[] args) {    HashMap<String, ArrayList<String>> myHashMap = new HashMap<String, ArrayList<String>>();    ArrayList<String> catList = new ArrayList<String>();    catList.add(Meow);    myHashMap.put(Cat, catList);    ArrayList<String> tempList = myHashMap.get(Cat);    tempList.add(Purr);    // Prints {Cat=[Meow, Purr]} as expected.    System.out.println(myHashMap);    ArrayList<String> copyList = new ArrayList<String>();    copyList.add(tempList.get(0));    copyList.add(tempList.get(1));    // New list item    copyList.add(Drink milk);    tempList = copyList;    // Prints copyList: [Meow, Purr, Drink milk]    System.out.println(copyList:  + copyList);    // Prints tempList: [Meow, Purr, Drink milk]    System.out.println(tempList:  + tempList);    // Prints {Cat=[Meow, Purr]} - why not {Cat=[Meow, Purr, Drink milk]}?    System.out.println(myHashMap);  }}Because the lists are 'passed by reference' (I know that Java actually passes everything by value, but the lists are effectively passed by reference), why doesn't tempList = copyList make the HashMap value point to copyList?
Chains of references - an unexpected problem
java
Rather than take this as an off-topic request to debug your code (see stackoverflow for that) I'm going to take this as a question about how java references work.why doesn't tempList = copyList make the HashMap value point to copyList?Because the HashMap data structure doesn't even know that the tempList identifier exists.You have a similar question in this comments of the code about myHashMap:// Prints {Cat=[Meow, Purr]} - why not {Cat=[Meow, Purr, Drink milk]}? Before tempList = copyList things looked like this:copyList  ---> [Meow, Purr, Drink milk]  tempList  ---> [Meow, Purr]                     ^myHashMap ---> {Cat=/ }After tempList = copyList they look like this: copyList ---,> [Meow, Purr, Drink milk]             /  tempList --    [Meow, Purr]                     ^myHashMap ---> {Cat=/ }The way you change what Cat's value points at is the same way to created it in the first place.If you do this myHashMap.put(Cat, copyList); copyList ---,> [Meow, Purr, Drink milk]             /                  ^ tempList --    [Meow, Purr]  /                            /myHashMap ---> {Cat=  ------ }Now nothing points at [Meow, Purr].  It has been lost and is off to meet it's fate with the garbage collector.copyList, tempList, and Cat are not the list data structures.  They're things that point to those list data structures by storing their memory address.  My house has an address.  If you write that address down on a little slip of paper, walk around with it in your pocket, then take it out, erase it, and write down a new address that doesn't move my house anywhere.  It sends whoever gets that paper to a different house.It may be worth noting here that one of the reasons this stuff is so hard to keep straight is you tend to keep looking for something that doesn't exist here: identity.[Meow, Purr] isn't an identity.  It's just values in a list.  tempList isn't an identity. It's just an identifier that points to an address.  Even an address isn't an identity.  If I pick up my house and move it down the block it's the same house.  It's hard to even talk about this stuff when nothing has a reliable identity.  But if there were an identity here, it would be unique and immutable. It would belong to the data structure alone. 