Java Program to Convert Integer List to Integer Array

By IncludeHelp Last updated : February 4, 2024

Problem statement

Given an integer list, you have to write a Java program to convert the given integer list to an integer array.

Converting Integer List to Integer Array

To convert an integer list to an integer array, use the .stream() method and convert stream<Integer> to int[] (integer array).

Syntax

Below is the syntax to convert an integer list to an integer array:

Java Program to Convert Integer List to Integer Array

Below is a program that converts an integer list to an integer array.

// Converting Integer List to Integer Array

// Importing the packages
import java.util.Arrays;
import java.util.List;

// The Main Class
public class Main {
  public static void main(String[] args) {
    // Creating an List of integer type
    List < Integer > listInts = Arrays.asList(10, 20, 30, 40, 50);

    // Converting Integers list to integers array
    int[] intArray = listInts.stream()
      .mapToInt(Integer::intValue)
      .toArray();

    // Printing the array
    System.out.println("Array of integers : " + Arrays.toString(intArray));
  }
}

Output

The output of the above program is:

Array of integers : [10, 20, 30, 40, 50]

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.