Convert Array into Collection in Java



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

Problem Statement

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

Input

Input array: [Java, Python, Scala, Shell]

Output

After elements after converting the array to a list are: [Java, Python, Scala, Shell]

Using main() method

Following are the steps to convert the array into a collection using the main() method ?

  • Import all the necessary classes from java.util package.
  • Define an array input_array with elements "Java", "Python", "Scala", and "Shell".
  • Print the array using Arrays.toString(input_array).
  • Convert input_array to a list using Arrays.asList(input_array) and store it in result_list.
  • Print result_list to display the array elements as a collection.

Example

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

import java.util.*;
public class Demo {
   public static void main(String args[]){
      String input_array[] = { "Java", "Python", "Scala", "Shell" };
      System.out.println("The array is defined as: " + Arrays.toString(input_array));
      List result_list = Arrays.asList(input_array);
      System.out.println("\nAfter elements after converting the array to a list are: " + result_list);
   }
}

Output

The array is defined as: [Java, Python, Scala, Shell]

After elements after converting the array to a list are: [Java, Python, Scala, Shell]

Using encapsulation

Following are the steps to convert the array into a collection using encapsulation ?

  • Import all the necessary classes from the java.util package.
  • Define a static method convert_to_list that accepts an array parameter, converts it to a list using Arrays.asList(input_array), and prints the list.
  • In the main method, define the array input_array with elements "Java", "Python", "Scala", and "Shell", print the array, and call convert_to_list(input_array) to display it as a collection

Example

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

import java.util.*;
public class Demo {
   static void convert_to_list(String input_array[]){
      List result_list = Arrays.asList(input_array);
      System.out.println("\nAfter elements after converting the array to a list are: " + result_list);
   }
   public static void main(String args[]){
      String input_array[] = { "Java", "Python", "Scala", "Shell" };
      System.out.println("The array is defined as: " + Arrays.toString(input_array));
      convert_to_list(input_array);
   }
}

Output

The array is defined as: [Java, Python, Scala, Shell]

After elements after converting the array to a list are: [Java, Python, Scala, Shell]
Updated on: 2024-11-04T18:44:02+05:30

669 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements