BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Convert Integer List to int Array in Java

Last Updated: September 23, 2022 by Chaitanya Singh | Filed Under: java

In this guide, we will see how to convert Integer List to int Array in Java. There are several ways, you can do this conversion, we have covered following methods:

  • Using toArray() method
  • Using simple for loop
  • Using Stream API

1. Using toArray() method

This is one of the simplest way of doing the ArrayList to Array conversion. Here, we are using toArray() method of ArrayList class to convert the given Integer ArrayList to int array.

import java.util.*;
public class JavaExample {
  public static void main(String[] args) {

    //A List of integers
    List<Integer> list= new ArrayList<>();
    list.add(2);
    list.add(4);
    list.add(6);
    list.add(8);
    list.add(10);
    System.out.println("List Elements: "+list);
    // List to Array Conversion using toArray()
    Object arr[]=list.toArray();

    System.out.println("Array Elements: ");
    //Print array elements
    for(Object num : arr)
    {
      System.out.println(num);
    }
  }
}

Output:

List Elements: [2, 4, 6, 8, 10]
Array Elements: 
2
4
6
8
10

2. Using Simple for loop

In this example, we are using a simple for loop to convert Integer list to int array. We are iterating the list using for loop and assigning the list elements to array at every iteration. To fetch the list elements based on index, we are using get() method.

The steps are as follows:

  1. Create an array of same size. The size of list is obtained using size() method.
  2. Loop the list and get element at every iteration.
  3. Assign the element fetched from the list to the int array.
  4. Print array elements.
import java.util.*;
public class JavaExample {
  public static void main(String[] args) {

    //ArrayList declaration and initialization
    List<Integer> arrList= Arrays.asList(2, 4, 8, 16);

    //creating an array of same size
    int arr[] = new int[arrList.size()];

    //iterating ArrayList and adding each element
    //manually using get() method.
    for(int j =0;j<arrList.size();j++){
      arr[j] = arrList.get(j);
    }

    System.out.println("List Elements: "+ arrList);
    System.out.println("Array Elements:");
    //Displaying Array elements
    for(int i: arr)
    {
      System.out.println(i);
    }
  }
}

Output:

List Elements: [2, 4, 8, 16]
Array Elements:
2
4
8
16

3. Using Steam API

Here, we are using java 8 stream API. The steps followed in this program are:

  1. Convert the List<String> to Stream<String> using the stream() method.
  2. Convert Stream<String> to Stream<Integer> using mapToInt() method.
  3. Convert Stream<Integer> to int array using toArray() method.
import java.util.*;
class JavaExample {
  public static void main(String[] args)
  {
    // List of Integer type
    List<Integer> arrList = new ArrayList<>();
    arrList.add(1);
    arrList.add(3);
    arrList.add(5);
    arrList.add(7);
    arrList.add(9);

    System.out.println("ArrayList Elements: "+arrList);
    // stream() converts given ArrayList to stream
    // mapToInt() converts the obtained stream to IntStream
    // toArray() is used to return an array
    int[] arr = arrList.stream()
            .mapToInt(Integer::intValue)
            .toArray();

    System.out.println("Array Elements: ");
    // Print array elements
    for(int i: arr){
      System.out.println(i);
    }
  }
}

Output:

Convert Integer List to int array example
Main Article: Collections in Java

Top Related Articles:

  1. How to loop HashMap in java
  2. How to loop ArrayList in Java
  3. Add Multiple Items to an ArrayList in Java
  4. Java 9 – @SafeVarargs Annotation (with examples)
  5. How to get the last element of Arraylist?

Tags: Collections, Java-Array

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap