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]

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz