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

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ÂÂimportjava.util.*;importjava.util.stream.*;importjava.util.function.*;ÂÂclassGFG {   Â// Generic function to convert List of   Â// String to List of String   Âpublicstatic<T, U> List<U>   ÂconvertIntListToStringList(List<T> listOfInteger,                              ÂFunction<T, U> function)   Â{       ÂreturnlistOfInteger.stream()           Â.map(function)           Â.collect(Collectors.toList());   Â}   Âpublicstaticvoidmain(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ÂÂimportcom.google.common.base.Function;importcom.google.common.collect.Lists;importjava.util.*;importjava.util.stream.*;ÂÂclassGFG {   Â// Generic function to convert List of   Â// String to List of String   Âpublicstatic<T, U> List<U>   ÂconvertIntListToStringList(List<T> listOfInteger,                              ÂFunction<T, U> function)   Â{       ÂreturnLists.transform(listOfInteger, function);   Â}   Âpublicstaticvoidmain(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]



