$show=/label

Java Program To Copy(Add) All List Values To A new ArrayList

SHARE:

A quick and practical programming guide to copy all values from one list to another. Different ways are explored in this article with example programs.

1. Overview


In this tutorial, We'll be learning how to copy all list values to another new List or ArrayList in Java. Here our context copy means adding all values to a new List. how to copy list values to another list in java or Copy a List to Another List in Java can be done in many ways in java.
And We will keep tracking and show all the possible errors that encounter in our practice programs. Please read the entire article to get a better deeper understanding.

We have already written an article on ArrayList API Methods and its examples.

Java Program To Copy(Add) All List Values To A new ArrayList


We will be showing the example programs using constructor, addAll method, Using Java 8 and Java 10 concepts finally using Collections.copy() method. Hope this article will be interesting for you.

Note: ArrayList is not an Immutable object.


Let us start with the constructor first.

2. Using ArrayList Constructor


ArrayList has an overloaded constructor as below which takes Collection as an argument. So, we pass the original list to the constructor and creates a new list.

ArrayList(Collection<? extends E> c) 

Read the article on How to add values to ArrayList?

Example:

package com.javaprogramto.w3schools.programs.list;

import java.util.ArrayList;
import java.util.List;

public class ArrayListCopyContructor {

    public static void main(String[] args) {

        // Creating arraylist
        List<String> fruits = new ArrayList<>();

        // Adding values
        fruits.add("Orange");
        fruits.add("Apple");
        fruits.add("Grapes");

        System.out.println("Original fruits list values : " + fruits);

        // Copying values to new arraylist
        List<String> fruitsCopy = new ArrayList<>(fruits);
        
        // printing new list values.
        System.out.println("new fruitsCopy list values : "+fruitsCopy);

    }

}

Output:

Original fruits list values : [Orange, Apple, Grapes]
new fruitsCopy list values : [Orange, Apple, Grapes]

If we pass null to the ArrayList constructor then it produces a runtime exception saying "java.lang.NullPointerException".

List<String> fruitsCopy = new ArrayList<>(null);


Exception:


Exception in thread "main" java.lang.NullPointerException
    at java.util.ArrayList.<init>(ArrayList.java:178)
    at com.javaprogramto.w3schools.programs.list.ArrayListCopyContructor.main(ArrayListCopyContructor.java:21)

3. Using addAll() method


ArrayList API has a method addAll() which takes the collection as an argument.

public boolean addAll(Collection<? extends E> c)

Appends all of the elements in the specified collection to the end of this list

Example Code:

package com.javaprogramto.w3schools.programs.list;

import java.util.ArrayList;
import java.util.List;

public class ArrayListCopyAddAll {

    public static void main(String[] args) {

        // Creating arraylist
        List<String> originalFruitsList = new ArrayList<>();

        // Adding values
        originalFruitsList.add("Orange");
        originalFruitsList.add("Apple");
        originalFruitsList.add("Grapes");

        System.out.println("originalFruitsList : " + originalFruitsList);

        // Copying values to new arraylist
        List<String> fruitsCopyList = new ArrayList<>();
        fruitsCopyList.addAll(originalFruitsList);

        // printing new list values.
        System.out.println("New fruitsCopyList : " + fruitsCopyList);

        // Adding few more values to original list.
        originalFruitsList.add("Banana");

        System.out.println("Priting originalFruitsList after appending new values : " + originalFruitsList);
        System.out.println("Priting fruitsCopyList after appending new values : " + fruitsCopyList);
    }
}

Output:


originalFruitsList : [Orange, Apple, Grapes]
New fruitsCopyList : [Orange, Apple, Grapes]
Priting originalFruitsList after appending new values : [Orange, Apple, Grapes, Banana]
Priting fruitsCopyList after appending new values : [Orange, Apple, Grapes]

In the above program, I first created a list originalFruitsList and added three values to it. Next created a new ArrayList fruitsCopyList and invoked addAll(originalFruitsList) with passing originalFruitsList. Now, I printed both lists and have the same values in both lists. So, values are copied from originalFruitsList to fruitsCopyList.


Next, Added new values to the originalFruitsList and printed both list's values again. originalFruitsList only has the new value-added but fruitsCopyList is not having the newly added value. That means modifications to the originalFruitsList do not affect the newly created list from originalFruitsList.

This is the behavior when using the constructor and allAll() method.

4. Using Collections.copy() method


Collections.copy() method takes two List arguments such as dest and src. It copies values from the src list to the dest list. The order is the same as in the src list.
Copies all of the elements from one list into another.


public static <T> void copy(List<? super T> dest, List<? extends T> src)

package com.javaprogramto.w3schools.programs.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ArrayListCopyCollectionsCopy {

    public static void main(String[] args) {

        // Creating arraylist
        List<String> originalFruitsList = new ArrayList<>();

        // Adding values
        originalFruitsList.add("Orange");
        originalFruitsList.add("Apple");
        originalFruitsList.add("Grapes");

        System.out.println("originalFruitsList : " + originalFruitsList);

        // Copying values to new arraylist
        List<String> fruitsCopyList = new ArrayList<>();

        Collections.copy(fruitsCopyList, originalFruitsList);

        System.out.println("fruitsCopyList : " + fruitsCopyList);

    }
}

The above program gives the below runtime problem.

If the src and dest list size are diffrerent then it will throw IndexOutOfBoundsException.


java.lang.IndexOutOfBoundsException: Source does not fit in dest
    at java.util.Collections.copy(Collections.java:558)
    at com.javaprogramto.w3schools.programs.list.ArrayListCopyCollectionsCopy.main(ArrayListCopyCollectionsCopy.java:28)

src list size is 3 and the dest list size is 0. When using copy() method dest list size must be equal or greater than the src list.

If you try to set the ArrayList size to 3 using constructor will give the same IndexOutOfBoundsException.

List<String> fruitsCopyList = new ArrayList<>(3);

Because above line initializes the size of the ArrayList with three. That means a maximum of 3 elements values can be added but when it reaches threshold then it will be resized.

To make working our example, we must need to add a few dummy values to the destination list as below.

fruitsCopyList.add("1");
fruitsCopyList.add("2");
fruitsCopyList.add("3");

Now, this program runs successfully and produces the below output.

originalFruitsList : [Orange, Apple, Grapes]
fruitsCopyList : [Orange, Apple, Grapes]

Note: If the destination size is more than the src list then the reaming elements in the dest list will remain unchanged.

5. Using Java 8 Streams


Java 8 Introduced Streams and Lambda Expressions to reduce the code to be written and increases focus on business logic rather than syntax. Java 8 following functional programming similar to Scala.


Let us take a look at copying all values of the list to another list using java 8 streams api.

This example program covers the following.

A) Adding all values to a new list.
B) Skipping the first record and add reaming to the new list.
C) Find and add the fruit name length that equal to 5 to the new list.


package com.javaprogramto.w3schools.programs.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayListCopyCollectionsCopy {

    public static void main(String[] args) {

        // Creating arraylist
        List<String> originalFruitsList = new ArrayList<>();

        // Adding values
        originalFruitsList.add("Orange");
        originalFruitsList.add("Apple");
        originalFruitsList.add("Grapes");

        System.out.println("originalFruitsList : " + originalFruitsList);

        // copying all values to a new list.
        List<String> fruitesCopyList = originalFruitsList.stream().collect(Collectors.toList());
        System.out.println("All values into fruitesCopyList : " + fruitesCopyList);

        // skipping the first value.
        List<String> skippingFirstValue = originalFruitsList.stream().skip(1).collect(Collectors.toList());
        System.out.println("Skipping first value : " + skippingFirstValue);

        // fruit name length == 5
        List<String> fruitLength5 = originalFruitsList.stream().filter(fruit -> fruit.length() == 5)
                .collect(Collectors.toList());
        System.err.println("Fruites length == 5 :  fruitLength5 " + fruitLength5);

    }
}

Output:


originalFruitsList : [Orange, Apple, Grapes]
All values into fruitesCopyList : [Orange, Apple, Grapes]
Skipping first value : [Apple, Grapes]
Fruites length == 5 :  fruitLength5 [Apple]


6. Using Java 10 new method


JDK 10 released with the new method copyOf​() in List interface.

Syntax:

static <E> List<E> copyOf​(Collection<? extends E> coll)

Returns an unmodifiable List containing the elements of the given Collection, in its iteration order. The given Collection must not be null, and it must not contain any null elements. If the given Collection is subsequently modified, the returned List will not reflect such modifications.


List<T> copy = List.copyOf(list);

If the list is empty or the list has null values then it will throw NullPointerException.


7. List ConcurrentAccessException


A common problem when working with lists is ConcurrentAccessException. This could mean that we're modifying the list while we're trying to copy it, most likely in another thread.

To fix this issue we have to do either:


  •     Use a designed for concurrent access collection
  •     Lock the collection appropriately to iterate over it
  •     Find a way to avoid needing to copy the original collection



To avoid the concurrent access problems, you should use CopyOnWriteArrayList implementation which the copy of the original list when a thread is changing the values it creates a new copy for each mutative operation.

8. Conclusion


In this article, we have seen all the strategies to copy a list to another. These strategies include the addAll(), constructor, copy(), java 8 streams and java 10 new method.

And also what is the right way to work with ArrayList in concurrent environments.

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java Program To Copy(Add) All List Values To A new ArrayList
Java Program To Copy(Add) All List Values To A new ArrayList
A quick and practical programming guide to copy all values from one list to another. Different ways are explored in this article with example programs.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqky9lGlREeaPuo8cqkvFy26Bo3KkX-_nsUZkC6i1ExssC6XzZ3k2M7faKVjV-EpyXzSRJQDq-6MDbbmlIEfRHa3xh3Uw0Sm3IVW9fm6WGBe09wCE788bPwvE1rcqzWe307ytXJPd-Z14/s640/Java+Program+To+Copy%2528Add%2529+All+List+Values+To+A+new+ArrayList.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqky9lGlREeaPuo8cqkvFy26Bo3KkX-_nsUZkC6i1ExssC6XzZ3k2M7faKVjV-EpyXzSRJQDq-6MDbbmlIEfRHa3xh3Uw0Sm3IVW9fm6WGBe09wCE788bPwvE1rcqzWe307ytXJPd-Z14/s72-c/Java+Program+To+Copy%2528Add%2529+All+List+Values+To+A+new+ArrayList.png
JavaProgramTo.com
https://www.javaprogramto.com/2019/12/java-program-copy-list-values-to-new-arraylist.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2019/12/java-program-copy-list-values-to-new-arraylist.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content