Saturday, July 13, 2024
HomeLanguagesJavaHow to Add All Items From a Collection to an ArrayList in...

How to Add All Items From a Collection to an ArrayList in Java?

Given a Collection with some values, the task is to add all the items of this Collection to an ArrayList in Java.

Illustrations: 

Input: Collection = [1, 2, 3] 
Output: ArrayList = [1, 2, 3]
Input: Collection = [GFG, Geek, GeeksForGeeks] 
Output: ArrayList = [GFG, Geek, GeeksForGeeks] 

Approach:

  1. Get the Collection whose items are to be added into the ArrayList
  2. Create an ArrayList
  3. Add all the items of Collection into this ArrayList using ArrayList.addAll() method
  4. ArrayList with all the items of Collections have been created.

Example

Java




// Java Program to Add All Items from a collection
// to an ArrayList
 
// Importing required classes
import java.io.*;
import java.util.*;
import java.util.stream.*;
 
// Main class
class GFG {
 
    // Method 1
    // To add all items from a collection
    // to an ArrayList
    public static <T> ArrayList<T>
    createArrayList(List<T> collection)
    {
 
        // Creating an ArrayList
        ArrayList<T> list = new ArrayList<T>();
 
        // Adding all the items of Collection
        // into this ArrayList
        list.addAll(collection);
 
        return list;
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Getting array elements as list
        // and storing in a List object
        List<Integer> collection1 = Arrays.asList(1, 2, 3);
 
        // Printing elements in above List object
        System.out.println("ArrayList with all "
                           + "elements of collection "
                           + collection1 + ": "
                           + createArrayList(collection1));
 
        // Again creating another List class object
        List<String> collection2 = Arrays.asList(
            "GFG", "Geeks", "GeeksForGeeks");
 
        // Printing elements in above List object
        System.out.println("ArrayList with all"
                           + " elements of collection "
                           + collection2 + ": "
                           + createArrayList(collection2));
    }
}


Output

ArrayList with all elements of collection [1, 2, 3]: [1, 2, 3]
ArrayList with all elements of collection [GFG, Geeks, GeeksForGeeks]: [GFG, Geeks, GeeksForGeeks]

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments