Convert Set of Integer to Set of String in Java 8

In this post, we will see how to convert Set of Integer to Set of String in Java.


 

We can use Java 8 Stream API to convert Set<Integer> to Set<String>. Below are the complete steps:

  1. Convert Set<Integer> to Stream<Integer> using Set.stream().
     
  2. Convert Stream<Integer> to Stream<String> using Stream.map().
     
  3. Accumulate Stream<String> into Set<String> using Collectors.toSet().

 

Download   Run Code

Output:

[1, 2, 3, 4, 5]

 
Below is generic version of above program. It passes the method reference as an parameter to the generic function.

Download   Run Code

Output:

[1, 2, 3, 4, 5]

 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
Notify of