Convert List of Integer to List of String in Java
This post will discuss how to convert list of Integer to list of string in Java.
1. Using Java 8
We can use Java 8 Stream to convert List<Integer> to List<String>. Following are the complete steps:
- Convert
List<Integer>toStream<Integer>usingList.stream(). - Convert
Stream<Integer>toStream<String>usingStream.map(). - Accumulate
Stream<String>intoList<String>usingCollectors.toList().
The following program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList( 1, 2, 3, 4, 5 ); List<String> newList = list.stream() .map(String::valueOf) .collect(Collectors.toList()); System.out.println(newList); // [1, 2, 3, 4, 5] } } |
It is recommended to use method references for referring to an existing method instead of a lambda expression. For converting an integer to a string, we can pass String::valueOf to the map() method.
Using Generics:
Following is the generic version of the above program. It passes the method reference as a parameter to the generic function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Arrays; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; class Main { // Generic method to convert `List<Integer>` to `List<String>` public static <T, U> List<U> transform(List<T> list, Function<T, U> function) { return list.stream() .map(function) .collect(Collectors.toList()); } public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); List<String> newList = transform(list, String::valueOf); System.out.println(newList); // [1, 2, 3, 4, 5] } } |
Here’s an equivalent version without using the Stream API.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Function; class Main { // Generic method to convert `List<Integer>` to `List<String>` public static <T, U> List<U> transform(List<T> list, Function<T, U> function) { List<U> result = new ArrayList<>(); for (T t : list) { U u = function.apply(t); result.add(u); } return result; } public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); List<String> newList = transform(list, String::valueOf); System.out.println(newList); // [1, 2, 3, 4, 5] } } |
2. Using Guava Library
1. Guava’s Lists class provides the transform() method that returns a list that applies a specified method to each element of the specified list. This transformation happens in such a way that any changes to the original list will be reflected in the returned list, but no new items can be added to the returned list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.collect.Lists; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); List<String> newList = Lists.transform(list, String::valueOf); System.out.println(newList); // [1, 2, 3, 4, 5] } } |
2. We can also transform a list using Guava’s Iterables.transform(). It returns a view containing the result of applying a method to each list element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.collect.Iterables; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); Iterable<String> iterable = Iterables.transform(list, String::valueOf); System.out.println(iterable); // [1, 2, 3, 4, 5] } } |
Here’s how we can call Iterables.transform() with Java 7 and before:
|
1 2 3 4 5 6 |
Iterables.transform(list, new Function<Integer, String>() { @Override public String apply(Integer integer) { return String.valueOf(integer); } }); |
3. Similar to Iterables.transform(), we can also use Collections2.transform() that returns a collection, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import com.google.common.collect.Collections2; import java.util.Arrays; import java.util.Collection; import java.util.List; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); Collection<String> iterable = Collections2.transform(list, String::valueOf); System.out.println(iterable); // [1, 2, 3, 4, 5] } } |
Here’s how we can call Collections2.transform() with Java 7 and before:
|
1 2 3 4 5 6 |
Collections2.transform(list, new Function<Integer, String>() { @Override public String apply(Integer integer) { return String.valueOf(integer); } }); |
That’s all about converting Integer List to List of Java String.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)