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