The Wayback Machine - https://web.archive.org/web/20241007004558/https://www.geeksforgeeks.org/java-program-to-convert-integer-list-to-integer-array/
Open In App

Java Program to Convert Integer List to Integer Array

Last Updated : 25 Apr, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

There are many ways to convert integer List to ArrayList where in this article we will be discussing out 2 approaches as below:

  1. Using concept od streams in Java8
  2. Using Apache Commons Lang
  3. Using Guava Library

Method 1: Using concept od streams in Java8

So, first, we will get to know about how to convert integer list to array list using java 8. In java 8 there is a stream provided for converting a list of integer to an integer array.

Procedure:

  • Convert List<Integer> to Stream<Integer> using List.stream() —> Here List is calling stream() method.
  • Now we convert Stream<Integer> to int[].

Example:

Java




// Java Program to Convert Integer List to Integer Array
 
// Importing Arrays and List classes
// from java.util package
import java.util.Arrays;
import java.util.List;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an List of integer type and
        // customly inserting input elements a arguments
        List<Integer> list = Arrays.asList(1, 3, 5, 7, 9);
 
        // Converting Stream<Integer> to integer array
        // using stream() and mapToInt() methods, lately
        // storing them in an integer array
        int[] primitive = list.stream()
                              .mapToInt(Integer::intValue)
                              .toArray();
 
        // Print and display the integer array
        System.out.println(Arrays.toString(primitive));
    }
}


Output:

[1,3,5,7,9]

The time complexity is O(n), where n is the number of elements in the input List.

The auxiliary space is also O(n), where n is the number of elements in the input List.

Method 2: Using Apache Commons Lang

Apache Commons Lang’s ArrayUtils class provides toPrimitive() method that can convert an Integer array to primitive ints. We need to convert a list of integers to an Integer array first. We can use List.toArray() for easy conversion.

Procedure:

  1.  Use toPrimtive() method of Apache Common Lang’s
  2. Use List.toArray() method

Example:

Java




// Java Program to Convert Integer List to Integer Array
 
// Importing ArrayUtis class
// Importing Arrays and List class from java.util package
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of List class of integer type
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
 
        // Using toPrimtive() method of Apache Common Lang's
        int[] primitive = ArrayUtils.toPrimitive(
            list.toArray(new Integer[0]));
 
        // Print and display the integer array
        System.out.println(Arrays.toString(primitive));
    }
}


Output:

[1,2,3,4,5]

Method 3: Using Guava Library

Guava library is an open-source decentralized software development model which is a set of common libraries of java providing utility methods for collections, caching, primitives support, concurrency, string processing, and validations. Here we will be glancing at string processing in the guava library by converting integer list to integer array. It is depicted in below example as follows:

Example

Java




// Java Program to Convert List to Integer Array
// Using Guava Library
 
// Importing required classes
import com.google.common.primitives.Ints;
import java.util.Arrays;
import java.util.List;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of List class of integer type
        List<Integer> ints = Arrays.asList(3, 7, 8, 6, 1);
 
        // Converting a list of Integer to primitive integer
        // array and storing in integer array
        int[] primitive = Ints.toArray(ints);
 
        // Print and display the integer array via Guava
        // by converting it to string
        // via standard toString() method
        System.out.println(Arrays.toString(primitive));
    }
}


Output:

[3,7,8,6,1]

Time complexity: O(n) where n is size of given list

Auxiliary space: O(n)



Similar Reads

Program to Convert Set of Integer to Array of Integer in Java
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. Java 8 Stream API can be used to convert Set to Se
2 min read
Program to convert List of Integer to List of String in Java
The Java.util.List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes. Using Java 8 Stream API: A Stream is
3 min read
Program to convert List of String to List of Integer in Java
The Java.util.List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector, and Stack classes. 1. Using Java 8 Stream API: A Strea
3 min read
Java Program to Convert String to Integer Array
Strings - Strings in Java are objects that are supported internally by a char array. Since arrays are immutable, and strings are also a type of exceptional array that holds characters, therefore, strings are immutable as well. Integer Array - An array is a combination of the same type of variables. Likewise, a collection of integers (int) referred
3 min read
Converting Integer-String HashMap to String-Array-Integer HashMap in Java
Efficient Conversion of a HashMap<Integer, String> into another HashMap<String, Integer[]> involves organizing integer keys based on their corresponding string values. Here's a concise and efficient Java method to accomplish this transformation, focusing on clear and straightforward implementation. Step-by-Step Implementation of Convers
3 min read
Program to Convert Set of Integer to Set of String in Java
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. Java 8 Stream API can be used to convert Set to Se
3 min read
Program to convert set of String to set of Integer in Java
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. Java 8 Stream API can be used to convert Set to Se
2 min read
Java Program to convert boolean to integer
Given a boolean value, the task is to convert this boolean value into an integer value in Java. Examples: Input: boolean = true Output: 1 Input: boolean = false Output: 0 Approach: Get the boolean value to be converted.Check if boolean value is true or falseIf the boolean value is true, set the integer value as 1.Else if the boolean value is false,
2 min read
Java Program to convert integer to boolean
Given a integer value, the task is to convert this integer value into a boolean value in Java. Examples: Input: int = 1 Output: true Input: int = 0 Output: false Approach: Get the boolean value to be converted. Check if boolean value is true or false If the integer value is greater than equal to 1, set the boolean value as true. Else if the integer
2 min read
Java Program to Convert Integer Values into Binary
Given an integer in Java, your task is to write a Java program to convert this given integer into a binary number. Example: Input: = 45 Output: = 101101 Input: = 32 Output: = 100000 Integers: Integers are numbers whose base value is 10. The Integer or int data type is a 32-bit signed two’s complement integer. Its value-range lies between – 2,147,48
4 min read
Java Program to Convert an Array into a List
Given an array. Your task is to convert the given array into a list in Java. Examples: Input: Array: [Geeks, forGeeks, A computer Portal] Output: List: [Geeks, forGeeks, A computer Portal] Input: Array: [1, 2, 3, 4, 5] Output: List: [1, 2, 3, 4, 5] Arrays: An array is a group of like-typed variables that are referred to by a common name. An array c
6 min read
Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples
Most of the times, in competitive programming, there is a need to assign the variable, the maximum or minimum value that data type can hold, but remembering such a large and precise number comes out to be a difficult job. Therefore, Java has constants to represent these numbers, so that these can be directly assigned to the variable without actuall
3 min read
5 Ways to Convert Double to Integer in Java
Double is a primitive data type in Java that is used to represent double-precision floating point numbers. It uses the IEEE 754 standard for representing floating point numbers and has a default value of 0.0d. The double data type is the default data type for decimal numbers in Java and has a default size of 8 bytes. Example of Double double d1 = 1
5 min read
How to Convert a String Class to an Integer Class in Java?
String in Java is used to store the sequence of characters in Java. A string can contain a character, a word, a sentence, or a paragraph. Integer is a Wrapper class that is used to store Integer values in Java. The range of the Integer class is from -2,147,483,648 to 2,147,483,647 (-2^31 to 2^31 - 1). In this article, we will learn to Convert a Str
3 min read
Convert Double to Integer in Java
Given a Double real number. Write a Java program to convert the given double number into an Integer (int) in Java. Examples: Input: double = 3452.234Output: 3452Input: double = 98.23Output: 98Double: The double data type is a double-precision 64-bit IEEE 754 floating-point. Its value range is endless. The double data type is commonly used for decim
4 min read
Program to Convert Set to List in Java
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, i
4 min read
Program to Convert List to Map in Java
The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes. The java.util.Map interface represents a mappi
5 min read
Program to Convert a Vector to List in Java
Given a Vector, the task is to Convert Vector to List in Java Examples: Input: Vector: [1, 2, 3, 4, 5] Output: List: [1, 2, 3, 4, 5] Input : Vector = [a, b, c, d, e, f] Output : List = [a, b, c, d, e, f] Using Collections.list() method Syntax: List list = Collections.list(vec.elements()); Approach: Get the Vector Convert into list using Collections
2 min read
Java Program to Convert List to HashSet
The List interface provides a way to store the ordered collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. The HashSet class permits the null element. The class also offers constant time performance for the basic
4 min read
Program to Convert List to Stream in Java
The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes. A Stream is a sequence of objects that support
3 min read
How to Convert java.sql.Date to java.util.Date in Java?
If we have the Date object of the SQL package, then we can easily convert it into an util Date object. We need to pass the getTime() method while creating the util Date object. java.util.Date utilDate = new java.util.Date(sqlDate.getTime()); It will give us util Date object. getTime() method Syntax: public long getTime() Parameters: The function do
1 min read
Different Ways to Convert java.util.Date to java.time.LocalDate in Java
Prior to Java 8 for handling time and dates we had Date, Calendar, TimeStamp (java.util) but there were several performance issues as well as some methods and classes were deprecated, so Java 8 introduced some new concepts Date and Time APIs' (also known as Joda time APIs') present in java.time package. Java 7: Date, Calendar, TimeStamp present ins
3 min read
How to Convert java.util.Date to java.sql.Date in Java?
Date class is present in both java.util package and java.sql package. Though the name of the class is the same for both packages, their utilities are different. Date class of java.util package is required when data is required in a java application to do any computation or for other various things, while Date class of java.sql package is used whene
3 min read
How to Convert a LinkedHashSet into an Array and List in Java?
In Java, LinkedHashSet is a predefined class of Java that extends the HashSet and it implements the Set interface. It can be used to implement the doubly linked list of the elements in the set, and it provides the predictable iteration order. In this article, we will learn how to convert a LinkedHashSet into an Array and List in Java. Step-by-Step
3 min read
How to Convert a PriorityQueue into an Array, List, and Set in Java?
In Java, PriorityQueue is an implementation of the Queue interface. It means high-priority elements can be saved first compared to lower-priority elements in the queue, and it follows the natural order of elements. In this article, we will learn how to convert a PriorityQueue into an Array, List, and Set in Java. Approach to convert a PriorityQueue
3 min read
Convert List to Array in Java
The List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects where duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. Now here we are given a List be it any LinkedList or ArrayList of stri
5 min read
Integer.valueOf() vs Integer.parseInt() with Examples
Integer.parseInt():While operating upon strings, there are times when we need to convert a number represented as a string into an integer type. The method generally used to convert String to Integer in Java is parseInt(). This method belongs to Integer class in java.lang package. It takes a valid string as a parameter and parses it into primitive d
4 min read
Program to Convert Stream to an Array in Java
A Stream is a sequence of objects that support various methods which can be pipelined to produce the desired result. An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, t
3 min read
Program to convert Array to Set in Java
Array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In the case of primitives data types, the actual values are stored in contiguous memory locations. In case of objects of a class, the actual objects are st
7 min read
Program to convert Boxed Array to Stream in Java
An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case of objects of a class, the actual objects are sto
3 min read
Practice Tags :