I have below java code.
List<SomePojo> list = new ArrayList<SomePojo>(); //add 100 SomePojo objects to list.
Now list has 100 objects.
If i create one more instance as below:
List<SomePojo> anotherList = new ArrayList<SomePojo>(); anotherList .addAll(list);
It is possible to add all elements from one Java List into another List . You do so using the List addAll() method.
ArrayList<List<String>> keyHold = new ArrayList<List<String>>(); String cold[] = new String[]{"actions","cold","enabled"}; List<String> lCold = Arrays. asList(cold); lCold. add(Rows. getRow(rowNum, 0)); keyHold.
An object is only once in memory. Your first addition to list
just adds the object references.
anotherList.addAll
will also just add the references. So still only 100 objects in memory.
If you change list
by adding/removing elements, anotherList
won't be changed. But if you change any object in list
, then it's content will be also changed, when accessing it from anotherList
, because the same reference is being pointed to from both lists.
100, it will hold the same references. Therefore if you make a change to a specific object in the list
, it will affect the same object in anotherList
.
Adding or removing objects in any of the list will not affect the other.
list
and anotherList
are two different instances, they only hold the same references of the objects "inside" them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With