Program to Convert Set of Integer to Set of String in Java

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 to Set.

Algorithm:



  1. Get the set of integers.
  2. Convert Set of Integer to Stream of Integer. This is done using Set.stream().
  3. Convert Stream of Integer to Stream of String. This is done using Stream.map().
  4. Collect Stream of String into Set of String. This is done using Collectors.toSet().
  5. Return/Print the set of String.

Program 1: Using direct conversion.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to convert
// Set<Integer> to Set<String> in Java 8
  
import java.util.*;
import java.util.stream.*;
  
class GFG {
  
    public static void main(String args[])
    {
        // Create a set of integers
        Set<Integer> setOfInteger = new HashSet<>(
            Arrays.asList(1, 2, 3, 4, 5));
  
        // Print the set of Integer
        System.out.println("Set of Integer: " + setOfInteger);
  
        // Convert Set of integers to set of String
        Set<String> setOfString = setOfInteger.stream()
                                      .map(String::valueOf)
                                      .collect(Collectors.toSet());
  
        // Print the set of String
        System.out.println("Set of String: " + setOfString);
    }
}

chevron_right


Output:

Set of Integer: [1, 2, 3, 4, 5]
Set of String: [1, 2, 3, 4, 5]

Program 2: Using generic function.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to convert
// Set<Integer> to Set<String> in Java 8
  
import java.util.*;
import java.util.stream.*;
import java.util.function.Function;
  
class GFG {
  
    // Generic function to convert Set of 
    // Integer to Set of String
    public static <T, U> Set<U>
    convertIntSetToStringSet(Set<T> setOfInteger, 
                          Function<T, U> function)
    {
        return setOfInteger.stream()
            .map(function)
            .collect(Collectors.toSet());
    }
  
    public static void main(String args[])
    {
  
        // Create a set of integers
        Set<Integer> setOfInteger = new HashSet<>(
            Arrays.asList(1, 2, 3, 4, 5));
  
        // Print the set of Integer
        System.out.println("Set of Integer: " + setOfInteger);
  
        // Convert Set of integers to set of String
        Set<String> setOfString = convertIntSetToStringSet(
                                                      setOfInteger,
                                                      String::valueOf);
  
        // Print the set of String
        System.out.println("Set of String: " + setOfString);
    }
}

chevron_right


Output:

Set of Integer: [1, 2, 3, 4, 5]
Set of String: [1, 2, 3, 4, 5]


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.