Convert a List of Integers to List of String in Java

In this tutorial, we will learn how to convert a list of integers to list of string in Java.

Converting the following list of Integers to list of String

List of Integer : ( 20, 27, 25, -4)

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Converter
{
  public static void main(String args[])
  {
    List<Integer> int_nos = Arrays.asList( 20, 27, 25, -4);

    List<String> str_nos = int_nos.stream().map(s -> String.valueOf(s)).collect(Collectors.toList());

    System.out.println("List of String: "+str_nos);
  }
}



Output:

List of String: [20, 27, 25, -4]

Explanation: In the above Java program, I have converted the Integer datatype list into a String datatype list. In the main function, I have declared a list ‘int_nos’ of integer datatype that stores the numbers. Then I have declared a list ‘str_nos’ of string datatype that will store the converted list of integer. In the conversion logic, it uses the stream as a pipeline that transmits the elements from one list to another. The map method converts the integer numbers to string by parsing it. At the end, the list of string are displayed.

Leave a Reply

Your email address will not be published. Required fields are marked *