To convert a list of integers to a list of strings in Java, you can use the Stream API along with the map function to transform each integer to its string representation.
In this example,
intList
.stream()
method to create a stream from the list.map
function to apply a transformation to each integer in the stream.Integer.toString()
method.collect()
method.import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5);
List<String> stringList = intList.stream()
.map(Object::toString)
.collect(Collectors.toList());
System.out.println("List of strings: " + stringList);
}
}
List of strings: [1, 2, 3, 4, 5]
In this tutorial, we learned How to Convert a List of Integers to a List of Strings in Java language with well detailed examples.