How to Copy a Java.Util.List into Another Java.Util.List

How to copy a java.util.List into another java.util.List

Just use this:

List<SomeBean> newList = new ArrayList<SomeBean>(otherList);

Note: still not thread safe, if you modify otherList from another thread, then you may want to make that otherList (and even newList) a CopyOnWriteArrayList, for instance -- or use a lock primitive, such as ReentrantReadWriteLock to serialize read/write access to whatever lists are concurrently accessed.

How to copy java.util.list Collection

You may create a new list with an input of a previous list like so:

List one = new ArrayList()
//... add data, sort, etc
List two = new ArrayList(one);

This will allow you to modify the order or what elemtents are contained independent of the first list.

Keep in mind that the two lists will contain the same objects though, so if you modify an object in List two, the same object will be modified in list one.

example:

MyObject value1 = one.get(0);
MyObject value2 = two.get(0);
value1 == value2 //true
value1.setName("hello");
value2.getName(); //returns "hello"

Edit

To avoid this you need a deep copy of each element in the list like so:

List<Torero> one = new ArrayList<Torero>();
//add elements

List<Torero> two = new Arraylist<Torero>();
for(Torero t : one){
Torero copy = deepCopy(t);
two.add(copy);
}

with copy like the following:

public Torero deepCopy(Torero input){
Torero copy = new Torero();
copy.setValue(input.getValue());//.. copy primitives, deep copy objects again

return copy;
}

How to copy Java Collections list

Calling

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

creates a shallow copy of a within b. All elements will exist within b in the exact same order that they were within a (assuming it had an order).

Similarly, calling

// note: instantiating with a.size() gives `b` enough capacity to hold everything
List<String> b = new ArrayList<String>(a.size());
Collections.copy(b, a);

also creates a shallow copy of a within b. If the first parameter, b, does not have enough capacity (not size) to contain all of a's elements, then it will throw an IndexOutOfBoundsException. The expectation is that no allocations will be required by Collections.copy to work, and if any are, then it throws that exception. It's an optimization to require the copied collection to be preallocated (b), but I generally do not think that the feature is worth it due to the required checks given the constructor-based alternatives like the one shown above that have no weird side effects.

To create a deep copy, the List, via either mechanism, would have to have intricate knowledge of the underlying type. In the case of Strings, which are immutable in Java (and .NET for that matter), you don't even need a deep copy. In the case of MySpecialObject, you need to know how to make a deep copy of it and that is not a generic operation.


Note: The originally accepted answer was the top result for Collections.copy in Google, and it was flat out wrong as pointed out in the comments.

Copy a list to another list without losing the runtime type of original list

The best approach is to make the caller send in a function that will create a new list of the same type. The alternative would be to use reflection, which would begin a stream of dangerous assumptions.

The following two are examples of copy implementations adapted from your code:

private static <T> List<T> copy0(List<T> input, Supplier<List<T>> newListCreator) {
List<T> newList = newListCreator.get();
newList.addAll(input);

return newList;
}

private static <T> List<T> copy2(List<T> input, Supplier<List<T>> newListCreator) {
return input.stream().collect(Collectors.toCollection(newListCreator));
}

And you can call either in this way:

List<Integer> y = copy2(x, LinkedList::new);

Copying a basic liststring

List<String> myCopy = new ArrayList<String>(jpmcRouting);

does the job. Collections.copy is not what you want; that only works if the destination list already has the right number of elements.

How to clone ArrayList and also clone its contents?

You will need to iterate on the items, and clone them one by one, putting the clones in your result array as you go.

public static List<Dog> cloneList(List<Dog> list) {
List<Dog> clone = new ArrayList<Dog>(list.size());
for (Dog item : list) clone.add(item.clone());
return clone;
}

For that to work, obviously, you will have to get your Dog class to implement the Cloneable interface and override the clone() method.

How do I clone a generic List in Java?

ArrayList newArrayList = (ArrayList) oldArrayList.clone();

How to copy selected elements in a list to another list using stream

you can do this just by stream().map() but I defined a constructor for Element2 Class:

element2List = element1List.stream().map(element -> new Element2(element.getId(), element.getUid())).collect(Collectors.toList());


Related Topics



Leave a reply



Submit