I have an ArrayList<String> that I'd like to return a copy of. ArrayList has a clone method which has the following signature:
public Object clone()
After I call this method, how do I cast the returned Object back to ArrayList<String>?
I have an ArrayList<String> that I'd like to return a copy of. ArrayList has a clone method which has the following signature:
public Object clone()
After I call this method, how do I cast the returned Object back to ArrayList<String>?
Why would you want to clone? Creating a new list usually makes more sense.
List<String> strs;
...
List<String> newStrs = new ArrayList<>(strs);
Job done.
ArrayList(Collection<? extends E> c) meaning it doesn't matter what kind of list you use as an argument.ArrayList newArrayList = (ArrayList) oldArrayList.clone();
ArrayList<String> newArrayList = (ArrayList<String>) oldArrayList.clone();.List<String> instead of ArrayList<String> on the left-hand side. This is how collections should be used most of the time.This is the code I use for that:
ArrayList copy = new ArrayList (original.size());
Collections.copy(copy, original);
Hope is usefull for you
ArrayList use ArrayList<YourObject>ArrayList constructor?With Java 8 it can be cloned with a stream.
import static java.util.stream.Collectors.toList;
...
List<AnObject> clone = myList.stream().collect(toList());
toCollection may be a better choice.Be advised that Object.clone() has some major problems, and its use is discouraged in most cases. Please see Item 11, from "Effective Java" by Joshua Bloch for a complete answer. I believe you can safely use Object.clone() on primitive type arrays, but apart from that you need to be judicious about properly using and overriding clone. You are probably better off defining a copy constructor or a static factory method that explicitly clones the object according to your semantics.
I think this should do the trick using the Collections API:
Note: the copy method runs in linear time.
//assume oldList exists and has data in it.
List<String> newList = new ArrayList<String>();
Collections.copy(newList, oldList);
List<MySerializableObject> copyList = new ArrayList<>(mMySerializableObjects.size()); It seems using copyList.addAll(original); is a good alternativeint arguments. I've no idea why you want to use this obscure static method instead of good old addAll.I find using addAll works fine.
ArrayList<String> copy = new ArrayList<String>();
copy.addAll(original);
parentheses are used rather than the generics syntax
new ArrayList<String>(original) ?To clone a generic interface like java.util.List you will just need to cast it. here you are an example:
List list = new ArrayList();
List list2 = ((List) ( (ArrayList) list).clone());
It is a bit tricky, but it works, if you are limited to return a List interface, so anyone after you can implement your list whenever he wants.
I know this answer is close to the final answer, but my answer answers how to do all of that while you are working with List -the generic parent- not ArrayList
Be very careful when cloning ArrayLists. Cloning in java is shallow. This means that it will only clone the Arraylist itself and not its members. So if you have an ArrayList X1 and clone it into X2 any change in X2 will also manifest in X1 and vice-versa. When you clone you will only generate a new ArrayList with pointers to the same elements in the original.
I am not a java professional, but I have the same problem and I tried to solve by this method. (It suppose that T has a copy constructor).
public static <T extends Object> List<T> clone(List<T> list) {
try {
List<T> c = list.getClass().newInstance();
for(T t: list) {
T copy = (T) t.getClass().getDeclaredConstructor(t.getclass()).newInstance(t);
c.add(copy);
}
return c;
} catch(Exception e) {
throw new RuntimeException("List cloning unsupported",e);
}
}
List implementation class has a public no-arg constructor. For instance, the Lists returns by Array.asList, List.of or List.subList probably don't.