Convert Collection into Array in Java



In this article, we will understand how to convert collection into array in Java. 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.

Problem Statement

Write a program in Java to convert collection into array. Below is a demonstration of the same ?

Input

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

Output

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

Different Approaches

Following are the approaches to convert collection into array ?

Using main method

Following are the steps to convert collection into array ?

  • Import necessary classes that is ArrayList from java.util.
  • Create an ArrayList and add string elements to it.
  • Convert the list to an array using toArray(new String[0]).
  • Iterate over the array using for loop and print each element.

Example

Here, we bind all the operations together under the main() method ?

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

Using object oriented programming

Following are the steps to

  • Import necessary classes that is ArrayList from java.util.
  • Create a method convert_to_array to convert the list to an array and print it.
  • In the main method, create an ArrayList and add string elements.
  • Call the convert_to_array method to perform the conversion and print the result.

Example

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: 2024-09-13T10:21:53+05:30

861 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements