在Java中将整数集合转换为字符串集合的程序
假设以下是我们的整数集-
Set<Integer> setInteger = new HashSet<>(Arrays.asList(100, 200, 300, 500, 600, 800, 1000));
现在,让我们将其转换为SetofString-
Set<String> setStr = setInteger.stream().map(String::valueOf).collect(Collectors.toSet());
示例
以下是在Java中将整数集转换为字符串集的程序-
import java.util.*; import java.util.stream.*; import java.util.function.Function; public class Demo { public static void main(String args[]) { Set<Integer> setInteger = new HashSet<>(Arrays.asList(100, 200, 300, 500, 600, 800, 1000)); System.out.println("Set = " + setInteger); Set<String> setStr = setInteger.stream().map(String::valueOf).collect(Collectors.toSet()); System.out.println("New Set (String) = " + setStr); } }
输出结果
Set = [800, 100, 500, 200, 600, 1000, 300] New Set (String) = [100, 200, 300, 500, 600, 800, 1000]