Can we assign one list to another in Java?

Java 8Object Oriented ProgrammingProgramming

Let’s say the following is our array −

String[] str = { "P", "Q", "R", "S", "T", "U", "V", "W" };

Now set the elements of the above array to a new List −

int len = str.length; List<String>list1 = new ArrayList<String>(); for (int i = 0; i < len; i++)    list1.add(str[i]);

Consider a new List with no elements −

List<String>list2 = new ArrayList<String>(); for (int i = 0; i < len; i++)    list2.add("");

Now copy value from one list to another −

Collections.copy(list2,list1);

Example

 Live Demo

import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       String[] str = { "P", "Q", "R", "S", "T", "U", "V", "W" };       int len = str.length;       List<String>list1 = new ArrayList<String>();       for (int i = 0; i < len; i++)          list1.add(str[i]);       List<String>list2 = new ArrayList<String>();       for (int i = 0; i < len; i++)          list2.add("");       Collections.copy(list2,list1);       ListIterator<String>iterator = list2.listIterator();       System.out.println("New List...");       while (iterator.hasNext())          System.out.println(iterator.next());    } }

Output

New List... P Q R S T U V W

Published on 24-Apr-2019 09:16:25

In this tutorial, I will show you 5 different ways to copy a List to another List with an example.

  1. Using Constructor
  2. Using addAll() method
  3. Using Collections.copy() method
  4. Using Java 8
  5. Using Java 10

A simple way to copy a List is by using the constructor that takes a collection as its argument:

package net.javaguides.examples; import java.util.ArrayList; import java.util.List; /** * Different ways to copy a list into another list * * @author Ramesh Fadatare * */ public class CopyListExamples { public static void main(String[] args) { List < String > fruits = new ArrayList < > (); // Adding new elements to the ArrayList fruits.add("Banana"); fruits.add("Apple"); fruits.add("mango"); fruits.add("orange"); System.out.println(fruits); // using constructor List < String > copyFruits = new ArrayList < > (fruits); System.out.println(copyFruits); } }

[Banana, Apple, mango, orange] [Banana, Apple, mango, orange]

This approach isn't thread-safe. We may get ConcurrentAccessException error, to fix this problem we may need to use CopyOnWriteArrayList, in which all mutative operations are implemented by making a fresh copy of the underlying array.

Another approach to copy elements is using the addAll() method:

package net.javaguides.examples; import java.util.ArrayList; import java.util.List; /** * Different ways to copy a list into another list * * @author Ramesh Fadatare * */ public class CopyListExamples { public static void main(String[] args) { List < String > fruits = new ArrayList < > (); // Adding new elements to the ArrayList fruits.add("Banana"); fruits.add("Apple"); fruits.add("mango"); fruits.add("orange"); System.out.println(fruits); // using the addAll method List < String > copy = new ArrayList < > (); copy.addAll(fruits); System.out.println(copy); } }

[Banana, Apple, mango, orange] [Banana, Apple, mango, orange]

The Collections class consists exclusively of static methods that operate on or return collections.

One of them is a copy() method, which needs a source list and a destination list at least as long as the source.

This method copies all elements of the source list to the destination list. After the copy index of the elements in both source and destination lists would be identical.

The destination list must be long enough to hold all copied elements. If it is longer than that, the rest of the destination list's elements would remain unaffected.

package net.javaguides.examples; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * Different ways to copy a list into another list * * @author Ramesh Fadatare * */ public class CopyListExamples { public static void main(String[] args) { List < String > fruits1 = new ArrayList < > (); // Adding new elements to the ArrayList fruits1.add("Banana"); fruits1.add("Apple"); fruits1.add("mango"); fruits1.add("orange"); System.out.println(fruits1); // using the Collections.copy() method List < String > fruits2 = new ArrayList < > (fruits1.size()); fruits2.add("1"); fruits2.add("2"); fruits2.add("3"); fruits2.add("4"); Collections.copy(fruits2, fruits1); System.out.println(fruits2); } }

[Banana, Apple, mango, orange] [Banana, Apple, mango, orange]

Let's use Java 8 Stream APIs to copy List into another List:

package net.javaguides.examples; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** * Different ways to copy a list into another list * * @author Ramesh Fadatare * */ public class CopyListExamples { public static void main(String[] args) { List < String > fruits = new ArrayList < > (); // Adding new elements to the ArrayList fruits.add("Banana"); fruits.add("Apple"); fruits.add("mango"); fruits.add("orange"); System.out.println(fruits); // using Java 8 Stream APIs List < String > copy = fruits.stream() .collect(Collectors.toList()); System.out.println(copy); } }

[Banana, Apple, mango, orange] [Banana, Apple, mango, orange]

Finally, one of the last Java version allows us to create an immutable list containing the elements of the given Collection:

package net.javaguides.examples; import java.util.ArrayList; import java.util.List; /** * Different ways to copy a list into another list * * @author Ramesh Fadatare * */ public class CopyListExamples { public static void main(String[] args) { List < String > fruits = new ArrayList < > (); // Adding new elements to the ArrayList fruits.add("Banana"); fruits.add("Apple"); fruits.add("mango"); fruits.add("orange"); System.out.println(fruits); // using Java 8 copyOf() method List < String > copy = List.copyOf(fruits); System.out.println(copy); } }

[Banana, Apple, mango, orange] [Banana, Apple, mango, orange]

In this tutorial, we've explored 5 different ways to copy a List to another List with different Java versions.

Collections Framework Java Collections Guide

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Video liên quan

Chủ đề