如何将 IntStream 转换为 List<Character>?

Ant*_*ony 4 java java-stream

我有一个对象,我将其限制为 30 个元素,并且我喜欢从流中的所有元素中IntStream获取一个。List<Character>int

提前致谢

azr*_*zro 6

您可以将每个映射int到一个,char然后收集到一个列表中,这将产生由其 ASCII 代码给出的字符列表

List<Character> r = IntStream.range(50, 80).mapToObj(a -> (char) a).collect(Collectors.toList());
System.out.println(r); // [2, 3, 4, 5, 6, 7, 8, 9, :, ;, <, =, >, ?, @, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O]
Run Code Online (Sandbox Code Playgroud)