-1

You will have to forgive me for asking this but I can't seem to find the exact answer to my question. I need to know how to copy this list:

List<String> jpmcRouting

into a new List (the name doesn't matter).

I have found answers on copying an ArrayList but not a standard list. I've tried a lot of solutions but nothing seems to work. I would appreciate any help!

jesric1029
  • 698
  • 3
  • 10
  • 33
  • 2
    You know that a 'standard' List is an interface right? That it cannot be instantiated – andrewdleach Mar 24 '16 at 18:23
  • What kind of `List` are you trying to copy? Do you want the result to have the same class as the original? – Paul Boddington Mar 24 '16 at 18:25
  • Possible duplicate of [How to copy a java.util.List into another java.util.List](http://stackoverflow.com/questions/14319732/how-to-copy-a-java-util-list-into-another-java-util-list) – randers Mar 24 '16 at 18:26
  • RAnders00 this is not a duplicate. I saw that answer and did not understand it.. The list has which makes no sense to me what kind of list that is and I tired the method of replacing with and it did not work. – jesric1029 Mar 24 '16 at 18:28

4 Answers4

2
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.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

The simplest way for me is:

List<String> copy = jpmcRouting.stream().collect(Collectors.toList());

if your result doesnt depend on order of elements in list, you can use

List<String> copy = jpmcRouting.parallelStream().collect(Collectors.toList());

it will work mutch faster, but it can change order of elements

Ján Яabčan
  • 743
  • 2
  • 10
  • 20
0

I could be wrong, but I think Collections.copy(b, a); will do the trick.

RMT
  • 7,040
  • 4
  • 25
  • 37
0

Do this:

List<String> b = new ArrayList<String>(jpmcRouting.size());
Collections.copy(b, jpmcRouting);
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183