Java Program to Convert an Array into Collection

In the previous article, we have seen Java Program to Create an Array and Fill it with Random Integer Values

In this article we are going to see how to convert an Array into Collection using Java programming language.

Java Program to Convert an Array into Collection

Array:

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Collection:

In Java, Collection is a framework which refers to collection of individual objects. This architecture stores and manipulates group of objects, classes and interfaces.

Let’s see different ways to convert an Array into Collection.

Method-1: Java Program to Convert an Array into Collection By Using For Loop

Approach:

  1. Initialize the array.
  2. Initialize the ArrayList.
  3. Iterate over the array and put each element in the ArrayList.

Program:

import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {
        //initialize array
        int[] array = { 1, 2, 3, 4, 5 };
        //initialize collection
        Collection<Integer> collection = new ArrayList<Integer>();
        //add elements to collection
        for (int i = 0; i < array.length; i++) 
        {
            collection.add(array[i]);
        }
        //print array and collection
        System.out.println("The array : " + Arrays.toString(array));
        System.out.println("The Collection : " + collection);
    }
}

Output:

The array: [1, 2, 3, 4, 5]
The Collection: [1, 2, 3, 4, 5]

Method-2: Java Program to Convert an Array into Collection By Using asList() Method

Approach:

  1. Create scanner class object.
  2. Ask use length of the array.
  3. Initialize the array with given size.
  4. Ask the user for array elements.
  5. Initialize the ArrayList.
  6. Convert the array to array list using asList() method.

Program:

import java.util.*;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        Integer[] arr = new Integer[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        // create collection object
        Collection<Integer> c = new ArrayList<>();
        // covert array to collection using asList() method
        c = Arrays.asList(arr);

        //print array and collection
        System.out.println("The array: " + Arrays.toString(arr));
        System.out.println("The Collection: " + c);
    }
}

Output:

Enter the size of array: 5
Enter array elements: 1 2 3 4 5
The array: [1, 2, 3, 4, 5]
The Collection: [1, 2, 3, 4, 5]

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Programs: