Convert List of String to Integer List in Java
This post will discuss how to convert list of string to list of Integer in Java. If the value of the specified string is negative (string prefixed with ASCII character ‘-‘ ), the solution should preserve the sign in the resultant integer.
1. Using Java 8
We can use Java 8 Stream to convert List<String> to List<Integer>. Following are the complete steps:
- Convert
List<String>toStream<String>usingList.stream(). - Convert
Stream<String>toStream<Integer>usingStream.map(). - Accumulate
Stream<Integer>intoList<Integer>usingCollectors.toList().
Note that map() operation will throw a NumberFormatException if the string does not contain a parsable integer.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; // Convert `List<String>` to `List<Integer>` class Main { public static void main(String[] args) { List<String> list = Arrays.asList( "-1" , "2", "3", "4", "5" ); List<Integer> newList = list.stream() .map(s -> Integer.parseInt(s)) .collect(Collectors.toList()); System.out.println(newList); // [-1, 2, 3, 4, 5] } } |
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; // Convert `List<String>` to `List<Integer>` class Main { public static void main(String[] args) { List<String> list = Arrays.asList( "-1" , "2", "3", "4", "5" ); List<Integer> newList = new ArrayList<>(); for (String s : list) { newList.add(Integer.parseInt(s)); } System.out.println(newList); // [-1, 2, 3, 4, 5] } } |
The lambda expression used in the above program does nothing but calls an existing method. It is recommended to refer to the existing method by name using method references.
For converting a string to an Integer, we can pass any one of the following method references to the map() method:
Integer::parseInt
Integer::valueOf
Integer::decode
NumberUtils::toInt // provided by Apache Commons Lang Math class
Using Generics:
Following is a 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 25 26 |
import java.util.Arrays; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; // Convert `List<String>` to `List<Integer>` class Main { // Generic method to convert list of string to list of Integer 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<String> list = Arrays.asList("-1", "2", "3", "4", "5"); List<Integer> newList = transform(list, Integer::parseInt); 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. The returned list is just a transformed view of the original list, and any changes to the original list will be reflected in the returned list. The transformation is done one-way, so we cannot add new items to the returned list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.collect.Lists; import java.util.Arrays; import java.util.List; // Convert `List<String>` to `List<Integer>` class Main { public static void main(String[] args) { List<String> list = Arrays.asList("-1", "2", "3", "4", "5"); List<Integer> newList = Lists.transform(list, Integer::parseInt); System.out.println(newList); // [-1, 2, 3, 4, 5] } } |
This method also throws a NumberFormatException if the string does not contain a parsable integer.
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.base.Function; import com.google.common.collect.Iterables; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> list = Arrays.asList("-1", "2", "3", "4", "5"); Iterable<Integer> iterable = Iterables.transform(list, Integer::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<String, Object>() { @Override public Integer apply(String s) { return Integer.valueOf(s); } }); |
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<String> list = Arrays.asList("-1", "2", "3", "4", "5"); Collection<Integer> collection = Collections2.transform(list, Integer::valueOf); System.out.println(collection); // [-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<String, Integer>() { @Override public Integer apply(String s) { return Integer.valueOf(s); } }); |
That’s all about converting List of String to Integer List in Java.
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 :)