Program to convert List of Integer to List of String in Java - GeeksforGeeks

11 Dec, 2018
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 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 List to List.Algorithm:Get the List of Integer.Convert List of Integer to Stream of Integer. This is done using List.stream().Convert Stream of Integer to Stream of String. This is done using Stream.map() and passing s -> String.valueOf(s) method as lambda expression.Collect Stream of String into List of String. This is done using Collectors.toList().Return/Print the List of String.Program:// Java Program to convert// List<Integer> to List<String> in Java 8  import java.util.*;import java.util.stream.*;import java.util.function.*;  class GFG {      // Generic function to convert List of    // String to List of String    public static <T, U> List<U>    convertIntListToStringList(List<T> listOfInteger,                               Function<T, U> function)    {        return listOfInteger.stream()            .map(function)            .collect(Collectors.toList());    }      public static void main(String args[])    {          // Create a List of Integer        List<Integer> listOfInteger = Arrays.asList(1, 2, 3, 4, 5);          // Print the List of Integer        System.out.println("List of Integer: " + listOfInteger);          // Convert List of Integer to List of String        List<String> listOfString = convertIntListToStringList(            listOfInteger,            s -> String.valueOf(s));          // Print the List of String        System.out.println("List of String: " + listOfString);    }}Output:List of String: [1, 2, 3, 4, 5]
List of Integer: [1, 2, 3, 4, 5]
Using Guava’s List.transform():Algorithm:Get the List of Integer.Convert List of Integer to List of String using Lists.transform(). This is done using passing s -> String.valueOf(s) method as lambda expression for transformation.Return/Print the List of String.Program:// Java Program to convert// List<Integer> to List<String> in Java 8  import com.google.common.base.Function;import com.google.common.collect.Lists;import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert List of    // String to List of String    public static <T, U> List<U>    convertIntListToStringList(List<T> listOfInteger,                               Function<T, U> function)    {        return Lists.transform(listOfInteger, function);    }      public static void main(String args[])    {          // Create a List of Integer        List<Integer> listOfInteger = Arrays.asList(1, 2, 3, 4, 5);          // Print the List of Integer        System.out.println("List of Integer: " + listOfInteger);          // Convert List of Integer to List of String        List<String> listOfString = convertIntListToStringList(            listOfInteger,            s -> String.valueOf(s));          // Print the List of String        System.out.println("List of String: " + listOfString);    }}Output:List of String: [1, 2, 3, 4, 5]
List of Integer: [1, 2, 3, 4, 5]
My Personal Notes
arrow_drop_upSave
Using Java 8 Stream API: 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 List to List.Algorithm:Get the List of Integer.Convert List of Integer to Stream of Integer. This is done using List.stream().Convert Stream of Integer to Stream of String. This is done using Stream.map() and passing s -> String.valueOf(s) method as lambda expression.Collect Stream of String into List of String. This is done using Collectors.toList().Return/Print the List of String.Program:// Java Program to convert// List<Integer> to List<String> in Java 8  import java.util.*;import java.util.stream.*;import java.util.function.*;  class GFG {      // Generic function to convert List of    // String to List of String    public static <T, U> List<U>    convertIntListToStringList(List<T> listOfInteger,                               Function<T, U> function)    {        return listOfInteger.stream()            .map(function)            .collect(Collectors.toList());    }      public static void main(String args[])    {          // Create a List of Integer        List<Integer> listOfInteger = Arrays.asList(1, 2, 3, 4, 5);          // Print the List of Integer        System.out.println("List of Integer: " + listOfInteger);          // Convert List of Integer to List of String        List<String> listOfString = convertIntListToStringList(            listOfInteger,            s -> String.valueOf(s));          // Print the List of String        System.out.println("List of String: " + listOfString);    }}Output:List of String: [1, 2, 3, 4, 5]
List of Integer: [1, 2, 3, 4, 5]

Java 8 Stream API can be used to convert List to List.
Algorithm:
Get the List of Integer.Convert List of Integer to Stream of Integer. This is done using List.stream().Convert Stream of Integer to Stream of String. This is done using Stream.map() and passing s -> String.valueOf(s) method as lambda expression.Collect Stream of String into List of String. This is done using Collectors.toList().Return/Print the List of String.
Get the List of Integer.
Convert List of Integer to Stream of Integer. This is done using List.stream().
Convert Stream of Integer to Stream of String. This is done using Stream.map() and passing s -> String.valueOf(s) method as lambda expression.
Collect Stream of String into List of String. This is done using Collectors.toList().
Return/Print the List of String.
Program:
// Java Program to convert// List<Integer> to List<String> in Java 8  import java.util.*;import java.util.stream.*;import java.util.function.*;  class GFG {      // Generic function to convert List of    // String to List of String    public static <T, U> List<U>    convertIntListToStringList(List<T> listOfInteger,                               Function<T, U> function)    {        return listOfInteger.stream()            .map(function)            .collect(Collectors.toList());    }      public static void main(String args[])    {          // Create a List of Integer        List<Integer> listOfInteger = Arrays.asList(1, 2, 3, 4, 5);          // Print the List of Integer        System.out.println("List of Integer: " + listOfInteger);          // Convert List of Integer to List of String        List<String> listOfString = convertIntListToStringList(            listOfInteger,            s -> String.valueOf(s));          // Print the List of String        System.out.println("List of String: " + listOfString);    }}
List of String: [1, 2, 3, 4, 5]
List of Integer: [1, 2, 3, 4, 5]

Using Guava’s List.transform():Algorithm:Get the List of Integer.Convert List of Integer to List of String using Lists.transform(). This is done using passing s -> String.valueOf(s) method as lambda expression for transformation.Return/Print the List of String.Program:// Java Program to convert// List<Integer> to List<String> in Java 8  import com.google.common.base.Function;import com.google.common.collect.Lists;import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert List of    // String to List of String    public static <T, U> List<U>    convertIntListToStringList(List<T> listOfInteger,                               Function<T, U> function)    {        return Lists.transform(listOfInteger, function);    }      public static void main(String args[])    {          // Create a List of Integer        List<Integer> listOfInteger = Arrays.asList(1, 2, 3, 4, 5);          // Print the List of Integer        System.out.println("List of Integer: " + listOfInteger);          // Convert List of Integer to List of String        List<String> listOfString = convertIntListToStringList(            listOfInteger,            s -> String.valueOf(s));          // Print the List of String        System.out.println("List of String: " + listOfString);    }}Output:List of String: [1, 2, 3, 4, 5]
List of Integer: [1, 2, 3, 4, 5]
My Personal Notes
arrow_drop_upSave
Algorithm:
Get the List of Integer.Convert List of Integer to List of String using Lists.transform(). This is done using passing s -> String.valueOf(s) method as lambda expression for transformation.Return/Print the List of String.
Get the List of Integer.
Convert List of Integer to List of String using Lists.transform(). This is done using passing s -> String.valueOf(s) method as lambda expression for transformation.
Return/Print the List of String.
Program:
// Java Program to convert// List<Integer> to List<String> in Java 8  import com.google.common.base.Function;import com.google.common.collect.Lists;import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert List of    // String to List of String    public static <T, U> List<U>    convertIntListToStringList(List<T> listOfInteger,                               Function<T, U> function)    {        return Lists.transform(listOfInteger, function);    }      public static void main(String args[])    {          // Create a List of Integer        List<Integer> listOfInteger = Arrays.asList(1, 2, 3, 4, 5);          // Print the List of Integer        System.out.println("List of Integer: " + listOfInteger);          // Convert List of Integer to List of String        List<String> listOfString = convertIntListToStringList(            listOfInteger,            s -> String.valueOf(s));          // Print the List of String        System.out.println("List of String: " + listOfString);    }}
List of String: [1, 2, 3, 4, 5]
List of Integer: [1, 2, 3, 4, 5]

Java - util package
java-list
Java-List-Programs
java-stream
Java-Stream-programs
Java-String-Programs
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Interfaces in Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Initializing a List in Java
Convert a String to Character Array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class