Cast an array of Strings to a List of Integers in Java

Summary of this article

As the title says, it is a trick that can be used when you are given a group of numerical values separated by spaces in standard input and want to replace them with Integer at once. (As a side note, in the case of Java, if you use nextInt () of java.util.Scanner, you can receive the standard input directly as an Integer, so I think that it will be less thankful in the context of receiving from the standard input.)

Target audience

Those who want to know how to make List a group of numbers passed as standard input in Java

To convert String [] to List

Conclusion: You can do it using the stream API

You can do this by using streamApi as shown below. As a miso, use map (Integer :: valueOf) to convert to Integer, and Collectors.toList () to make the final result a List.

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) throws Exception {
        String[] strArray = "1 2 3 4 5".split(" ");
        for( Integer i : strArrayToIntList(strArray) ) {
            System.out.println(i * i);
    private static List<Integer> strArrayToIntList(String[] strArray) {
        List<Integer> intList =
        return intList;

Recommended Posts

Cast an array of Strings to a List of Integers in Java
Convert an array of strings to numbers
How to output a list of strings in JSF as comma-separated strings
I want to ForEach an array with a Lambda expression in Java
[Java] How to search for a value in an array (or list) with the contains method
[Java] How to convert one element of a String type array to an Int type
How to convert a file to a byte array in Java
Convert a Java byte array to a string in hexadecimal notation
How to change a string in an array to a number in Ruby
I tried to make a client of RESAS-API in Java
Generate Stream from an array of primitive types in Java
[Ruby] How to batch convert strings in an array to numbers
Sort a List of Java objects
List of members added in Java 9
List of types added in Java 9
[Java] Conversion from array to List
How to make a Java array
How to concatenate strings in java
A program (Java) that outputs the sum of odd and even numbers in an array
[Ruby] I want to put an array in a variable. I want to convert to an array
Sorting a list with an int array as an element (Java) (Comparator)
How to get the length of an audio file in java
Effective Java Item 25 Select a list from an array First half
Why assign an ArrayList to a List
When seeking multiple in a Java array
About the method to convert a character string to an integer / decimal number (cast data) in Java
Ability to display a list of products
The story of forgetting to close a file in Java and failing
[Java] How to turn a two-dimensional array with an extended for statement
How to convert an array of Strings to an array of objects with the Stream API
How to get the absolute path of a directory running in Java
[Java] [For beginners] How to insert elements directly in a 2D array
[Java] From two Lists to one array list
Two ways to start a thread in Java + @
[java] Summary of how to handle character strings
Stuck in an enum in front of a blacksmith
I want to send an email in Java.
How to display a web page in Java
Measure the size of a folder in Java
Code to escape a JSON string in Java
Try to create a bulletin board in Java
A quick review of Java learned in class
[Java] Get a random value from an array
How to solve an Expression Problem in Java
[Swift] How to get the number of elements in an array (super basic)
How to make a judgment method to search for an arbitrary character in an array
How to ZIP a JAVA CSV file and manage it in a Byte array
Rails6 I want to make an array of values with a check box
When using a list in Java, java.awt.List comes out and an error occurs
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
[java] sort in list
How to create a Java environment in just 3 seconds
Sample code to convert List to List <String> in Java Stream
How to use an array for a TreeMap key
java: How to write a generic type list [Note]
I tried to create a Clova skill in Java
How to create a data URI (base64) in Java
How to save a file with the specified extension under the directory specified in Java to the list
I tried to make a login function in Java
Get a list of classes in a Guava specific package
About Java Array List