To convert a list of strings to a list of integers in Java, you can use a loop to iterate over each string and parse it to an integer using the Integer.parseInt()
method.
In this example,
stringList
.Integer.parseInt()
method.import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> stringList = List.of("1", "2", "3", "4", "5");
List<Integer> intList = new ArrayList<>();
for (String str : stringList) {
intList.add(Integer.parseInt(str));
}
System.out.println("List of integers: " + intList);
}
}
List of integers: [1, 2, 3, 4, 5]
In this tutorial, we learned How to Convert a List of Strings to a List of Integers in Java language with well detailed examples.