Convert Set of String to Set of Integer in Java 8 and above
This post will discuss how to convert Set of String to Set of Integer in Java 8 and above. If the value of the specified string is negative, the solution should preserve the sign in the resultant integer.
We can use Java 8 Stream to convert Set<String> to Set<Integer>. The following are the complete steps:
- Convert
Set<String>toStream<String>usingSet.stream(). - Convert
Stream<String>toStream<Integer>usingStream.map(). - Accumulate
Stream<Integer>intoSet<Integer>usingCollectors.toSet().
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.Set; import java.util.HashSet; import java.util.stream.Collectors; class Main { public static void main(String[] args) { Set<String> set = new HashSet<>(Arrays.asList("-1" , "2", "-3", "4")); Set<Integer> ints = set.stream() .map(s -> Integer.parseInt(s)) .collect(Collectors.toSet()); System.out.println(ints); // [-1, 2, -3, 4] } } |
The lambda expression used in the above program does nothing but calls an existing method. 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)
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 |
import java.util.Arrays; import java.util.Set; import java.util.HashSet; import java.util.function.Function; import java.util.stream.Collectors; class Main { // Generic method to convert `Set<String>` to `Set<Integer>` public static <T, U> Set<U> transform(Set<T> set, Function<T, U> function) { return set.stream() .map(function) .collect(Collectors.toSet()); } public static void main(String[] args) { Set<String> set = new HashSet<>(Arrays.asList("-1" , "2", "-3", "4")); Set<Integer> ints = transform(set, Integer::parseInt); System.out.println(ints); // [-1, 2, -3, 4] } } |
This is equivalent to:
|
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.Arrays; import java.util.HashSet; import java.util.Set; import java.util.function.Function; class Main { // Generic method to convert `Set<String>` to `Set<Integer>` public static <T, U> Set<U> transform(Set<T> set, Function<T, U> function) { Set<U> result = new HashSet<>(); for (T t : set) { U u = function.apply(t); result.add(u); } return result; } public static void main(String[] args) { Set<String> set = new HashSet<>(Arrays.asList("-1" , "2", "-3", "4")); Set<Integer> ints = transform(set, Integer::parseInt); System.out.println(ints); // [-1, 2, -3, 4] } } |
That’s all about converting Set of String to Set of Integer 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 :)