183

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>?

7
  • 17
    No, this is a valid question. Java does not support "true" generics, with runtime type erasure and all, so these kinds of details can be tricky. Furthermore, the Cloneable interface and the Object.clone() method mechanism is similarly confusing. Commented Sep 10, 2008 at 18:19
  • 1
    OK, I mostly do C# where this is really easy. Please let me know if you want me to remove the comments from this question. Commented Sep 10, 2008 at 18:21
  • 1
    You can leave the comment. I think my edits explained what I was having trouble with. Commented Sep 10, 2008 at 18:23
  • 2
    Your comment is OK, if a bit condescending. I imagine a lot of hoops that Java developers have to jump through seem silly to .NET developers. Commented Sep 10, 2008 at 18:24
  • 1
    @Oscar, he wants to clone, and not to invoke the clone command. It may not be the same it the clone doesn't really clones. I think this is the point. This is indeed a tricky question. Commented Mar 4, 2013 at 12:54

14 Answers 14

356

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.

Sign up to request clarification or add additional context in comments.

14 Comments

You may not know what type of List it is. It might be a LinkedList, MyOwnCustomList or a subclass of ArrayList, in which case newing an ArrayList would be the incorrect type.
Would I care which implementation the original list used? I probably care which implementation the new list uses.
@Steve Kuo: The signature is ArrayList(Collection<? extends E> c) meaning it doesn't matter what kind of list you use as an argument.
I mean it isn't a deep copy, is it?
@YekhezkelYovel No, it wouldn't be.
|
65
ArrayList newArrayList = (ArrayList) oldArrayList.clone();

6 Comments

This will work fine for Strings (which is what the question asked for), but it is worth noting that ArrayList.clone will perform a shallow copy, so if there were mutable objects in the list, they will not be cloned (and changing one in one list will change that one in the other list as well.
You should avoid using raw types in anything but legacy code. You're better off using ArrayList<String> newArrayList = (ArrayList<String>) oldArrayList.clone();.
It's too bad ArrayList has a #clone method, but List itself doesn't. Sigh.
Not related but while at it: use List<String> instead of ArrayList<String> on the left-hand side. This is how collections should be used most of the time.
I could not use this method to duplicate an ArrayList. The clone() method was not recognized.
|
20

This is the code I use for that:

ArrayList copy = new ArrayList (original.size());
Collections.copy(copy, original);

Hope is usefull for you

3 Comments

And avoid using raw types.. so instead of ArrayList use ArrayList<YourObject>
docs.oracle.com/javase/8/docs/api/java/util/… Doesn't work because the list sizes are different.
Why wouldn't you just use the copying overload of the ArrayList constructor?
18

With Java 8 it can be cloned with a stream.

import static java.util.stream.Collectors.toList;

...

List<AnObject> clone = myList.stream().collect(toList());

5 Comments

Can be done like List<AnObject> xy = new ArrayList<>(oldList);
This is not a deep copy, changes on one list's elements can be seen in another
Where in the question does it specify a deep copy is required? The assumption is that another collection containing the same objects is required. If you want to go down the deep copy route then you are opening up a whole new can of worms.
Not really a clone though, is it? From the API docs "There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned". Euw, don't want one of those. toCollection may be a better choice.
Why has this answer so many upvotes?! Question clearly asks for a clone, i.e. a deep copy, please downvote this answer!
17

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.

Comments

15

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);

5 Comments

Why not just use new ArrayList<String>(oldList) ?
I believe this wouldn't work, from doc * 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.
@GregDomjan not that i agree that this is the best way, but it's a way to do it. To get around your issue it's as simeple as this: List<String> newList = new ArrayList<>(oldList.size());
Not sure if it's related to Java 8, but even when specifying the size, still getting an exception IndexOutOfBoundsException: destination.size() < source.size(): 0 < 2 on List<MySerializableObject> copyList = new ArrayList<>(mMySerializableObjects.size()); It seems using copyList.addAll(original); is a good alternative
@GeneBo It isn't specifying the size. It's specifying the capacity, which is a very different thing. The joy of mystery int arguments. I've no idea why you want to use this obscure static method instead of good old addAll.
12

I find using addAll works fine.

ArrayList<String> copy = new ArrayList<String>();
copy.addAll(original);

parentheses are used rather than the generics syntax

6 Comments

That'll work for Strings, but not for mutable objects. You'll want to clone those as well.
Yeah, his question is for strings. And he has the problem of generics which don't really like the casting stuff in this case.
Also, ArrayList.clone will only do a shallow clone, so mutable objects in the list won't be cloned using that method either.
That should be ArrayList<String> btw. Also, you're probably better off using new ArrayList<String>(original) as it's less writing and just as clear.
Why not just use new ArrayList<String>(original) ?
|
8
List<String> shallowClonedList = new ArrayList<>(listOfStrings);

Keep in mind that this is only a shallow not a deep copy, ie. you get a new list, but the entries are the same. This is no problem for simply strings. Get's more tricky when the list entries are objects themself.

Comments

7

If you want this in order to be able to return the List in a getter it would be better to do:

ImmutableList.copyOf(list);

Comments

4

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

2 Comments

you are assuming that it will always be an ArrayList which is not true
@JuanCarlosDiaz will, this is the question asked, it was about ArrayList, so I answered it for ArrayList :)
3

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.

Comments

2

This should also work:

ArrayList<String> orig = new ArrayList<String>();
ArrayList<String> copy = (ArrayList<String>) orig.clone()

Comments

1
ArrayList first = new ArrayList ();
ArrayList copy = (ArrayList) first.clone ();

Comments

1

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);
      }
}

1 Comment

There's no guarantee that the 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.