Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy values, not references, of List<Integer> into another list?

Tags:

Namely, without referencing the same object, I need to copy values of elements of one list into another list. These are the lists:

List<Integer> listA = new ArrayList<Integer>(); List<Integer> ListB = new ArrayList<Integer>();  listA = (added some values);  listB = (do what?)... 

PS. I appologize for beginner's question, but I have never done such a thing.

like image 817
sandalone Avatar asked May 03 '12 07:05

sandalone


People also ask

How do you copy values from one list to another?

Another approach to copying elements is using the addAll method: List<Integer> copy = new ArrayList<>(); copy. addAll(list); It's important to keep in mind whenever using this method that, as with the constructor, the contents of both lists will reference the same objects.

Which method is used to copy one list to into another list?

Using the copy() method The Python List copy() is an inbuilt method copy used to copy all the elements from one list to another.

How do I clone one list and store it in a new list?

Create an empty list. Loop through each element of the original list, and invoke the SerializationUtils. clone() method of the serializable objects (which returns the class instance) and use the add() method to append it to the new list.

How do I convert a list from one type to another in Java?

As an alternative to the iterator pattern, you can use a abstract generic mapper class, and only override the transform method: create a generic collection mapper for any data type. [optional] create a library of methods that transform between different data types (and override the method) use that library.


2 Answers

There is absolutely no reason to make a copy of an Integer. Integer is an immutable class. This means that its value is set when the Integer instance is created, and can never change. An Integer reference can thus be shared by multiple lists and threads without fear, because there's no way anybody can change its value. Your question thus makes no real sense.

To create an ArrayList b containing the same Integers as another List a, just use the following code:

List<Integer> b = new ArrayList<Integer>(a); 

Sure the Integer won't be cloned, but this is a good thing, because cloning them is completely unnecessary.

like image 175
JB Nizet Avatar answered Oct 10 '22 11:10

JB Nizet


You can try and give a look at the Collections.copy method:

public static void copy(List dest, List src)

Copies all of the elements from one list into another. After the operation, the index of each copied element in the destination list will be identical to its index in the source list. The destination list must be at least as long as the source list. If it is longer, the remaining elements in the destination list are unaffected. This method runs in linear time.

Parameters: dest - The destination list. src - The source list.

Note: The above should work for simple data types such as Integers, however, if you have your own objects which might in turn reference other objects, you will have to iterate over each object and copy it separately.

like image 25
npinti Avatar answered Oct 10 '22 11:10

npinti