Java Program to Convert Collection into Array


In this article, we will understand how to convert collection into array. The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.

Below is a demonstration of the same −

Suppose our input is

Input list: [Java , program , is , fun]

The desired output would be

The result after converting to an array is:
Java program is fun

Algorithm

Step 1 - START
Step 2 - Declare a list namely input_list, a string array namely result_string.
Step 3 - Define the values.
Step 4 - Convert the list to an array of strings by assigning input_list.toArray(new String[0]) to the result_string array.
Step 5 - Display the result.
Step 6 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

import java.util.ArrayList;
import java.util.*;
public class Demo {
   public static void main(String[] args){
      System.out.println("Required packages have been imported");
      List<String> input_list = new ArrayList<String>();
      input_list.add("Java ");
      input_list.add("program ");
      input_list.add("is ");
      input_list.add("fun");
      System.out.println("The list is defined as:" + input_list);
      System.out.println("\nThe result after converting to an array is:");
      String[] result_string = input_list.toArray(new String[0]);
      for (int i = 0; i < result_string.length; i++) {
         String element = result_string[i];
         System.out.print(element);
      }
   }
}

Output

Required packages have been imported
The list is defined as:[Java , program , is , fun]

The result after converting to an array is:
Java program is fun

Example 2

Here, we encapsulate the operations into functions exhibiting object oriented programming.

import java.util.ArrayList;
import java.util.*;
public class Demo {
   static void convert_to_array(List<String> input_list){
      System.out.println("\nThe result after converting to an array is:");
      String[] result_array = input_list.toArray(new String[0]);
      for (int i = 0; i < result_array.length; i++) {
         String element = result_array[i];
         System.out.print(element);
      }
   }
   public static void main(String[] args){
      System.out.println("Required packages have been imported");
      List<String> input_list = new ArrayList<String>();
      input_list.add("Java ");
      input_list.add("program ");
      input_list.add("is ");
      input_list.add("fun");
      System.out.println("The list is defined as:" + input_list);
      convert_to_array(input_list);
   }
}

Output

Required packages have been imported
The list is defined as:[Java , program , is , fun]

The result after converting to an array is:
Java program is fun

Updated on: 30-Mar-2022

431 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements