VOOZH about

URL: https://www.geeksforgeeks.org/java/program-to-convert-set-of-string-to-set-of-integer-in-java/

⇱ Program to convert set of String to set of Integer in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to convert set of String to set of Integer in Java

Last Updated : 11 Jul, 2025
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. Java 8 Stream API can be used to convert Set. Algorithm:
  1. Get the set of String.
  2. Convert Set of String to Stream of String. This is done using Set.stream().
  3. Convert Stream of String to Stream of Integer. This is done using Stream.map() and passing Integer.parseInt() method as lambda expression.
  4. Collect Stream of Integer into Set of Integer. This is done using Collectors.toSet().
  5. Return/Print the set of String.
Program 1: Using direct conversion.
Output:
Set of String: [1, 2, 3, 4, 5]
Set of Integer: [1, 2, 3, 4, 5]
Program 2: Using generic function.
Output:
Set of String: [1, 2, 3, 4, 5]
Set of Integer: [1, 2, 3, 4, 5]
Comment