title: When doing addAll on other arraylist, both arraylists references same elements
body: <p>When I add all the contents of an <code>ArrayList</code> to another <code>ArrayList</code>, the other <code>ArrayList</code> contains references to elements of the first <code>ArrayList</code>. Hence, when I change an element in either of the <code>List</code>s, the corresponding element in the other <code>List</code> also changes. This clearly means <code>addAll</code> assigns references to the new <code>List</code>.</p>
  
  <p>Reference code:</p>
  
  <pre><code>ArrayList&lt;StringBuffer&gt; a = new ArrayList&lt;&gt;();
  a.add(new StringBuffer("1"));
  a.add(new StringBuffer("2"));
  a.add(new StringBuffer("3"));
  
  ArrayList&lt;StringBuffer&gt; b = new ArrayList&lt;&gt;();
  b.addAll(a);
  
  System.out.println("Arralists" + " before: ");
  System.out.println("A arraylist" + a.toString());
  System.out.println("B arraylist" + b.toString() + "\n\n");
  
  a.set(0, a.get(0).replace(0, 1, String.valueOf(4)));
  
  System.out.println("Arralists after " + "updating a: ");
  System.out.println("A arraylist" + a.toString());
  System.out.println("B arraylist" + b.toString() + "\n\n");
  </code></pre>
  
  <p>Corresponding logs:</p>
  
  <pre><code>Arralists before: 
  A arraylist[1, 2, 3]
  B arraylist[1, 2, 3]
  
  
  Arralists after updating a: 
  A arraylist[4, 2, 3]
  B arraylist[4, 2, 3]
  </code></pre>
  
  <p>I want to know the best way to avoid references and create a copy array.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I want to do this in android. @Eran suggested an answer with Java8 but compatibility issues come up with that. Any help will be appreciated.</p>