[英]Printing the letters from A to Z using a Java stream
I have this code, but it gives me an error: 我有这个代码,但它给了我一个错误:
Type mismatch: cannot convert from int to Character
类型不匹配:无法从int转换为Character
Stream.iterate('a', i -> i + 1).limit(26).forEach(System.out::println);
Although it is fine to write int i = 'a';
虽然可以写
int i = 'a';
I know I can write it like this, but that seems like too much code for a simple task. 我知道我可以像这样写它,但对于一个简单的任务来说,这似乎太多了。
Stream.iterate('a', i -> (char)(i + 1)).limit(26).forEach(System.out::println);
Why is the Java type inference failing? 为什么Java类型推断失败?
The reason why i -> i + 1
does not compile is because you're attempting to implicitly convert an int
to a Character
which the compiler cannot do itself alone. i -> i + 1
不编译的原因是因为你试图隐式地将int
转换为编译器不能单独执行的Character
。
In other words, you can think of Stream.iterate('a', i -> i + 1)
as: 换句话说,您可以将
Stream.iterate('a', i -> i + 1)
视为:
Stream.iterate('a', (Character i) -> {
int i1 = i + 1;
return i1; // not possible
});
As you have noted, explicitly casting to char
solves it: 如你所知, 显式转换为
char
解决了它:
Stream.iterate('a', i -> (char)(i + 1))...
Btw this is better done as: 顺便说一句,这样做更好:
IntStream.rangeClosed('a', 'z').forEach(c -> System.out.println((char)c));
This is better because: 这更好,因为:
h
with the use of iterate
you'd have to do more brain processing than just entering h
as the upper bound with rangeClosed
because you'd need to find the number to truncate the infinite stream upon. iterate
停止说字母h
,你必须做更多的脑处理,而不仅仅是输入h
作为rangeClosed
的上限,因为你需要找到截断无限流的数字。 iterate
generates an infinite stream which in this specific case has more overhead than the finite one with rangeClosed
. iterate
生成一个无限流,在这种特定情况下,它比具有rangeClosed
的有限流具有更多的开销。 Further, it's far easier to run IntStream.rangeClosed
in parallel, not that you want to in this specific case but it's something to keep in mind. IntStream.rangeClosed
要容易IntStream.rangeClosed
,而不是在这种特定情况下你想要的,但要记住这一点。 here is some discussion on Generators as sources by Brian Goetz. etc... 等等...
How about just: 怎么样:
Stream.iterate('a', i -> ++i).limit(26).forEach(System.out::println);
i -> i + 1
does not work because i
is a Character
and i + 1
causes an implicit narrowing conversion ( JLS 5.1.3 ), which is not allowed. i -> i + 1
不起作用,因为i
是一个Character
而i + 1
导致隐式缩小转换( JLS 5.1.3 ),这是不允许的。 You can explicitly cast it as was shown. 您可以按照显示的方式显式地投射它。 However
++i
works because (From JLS 15.15.1 ): 但是
++i
工作原因是(来自JLS 15.15.1 ):
Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable.
在添加之前,对值1和变量的值执行二进制数字提升(第5.6.2节)。 If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored.
如果必要,在存储之前,通过缩小的基元转换(第5.1.3节)和/或经过装箱转换(第5.1.7节)将总和缩小到变量的类型。
The ++
operator takes care of the narrowing conversion without us having to explicitly cast it ++
运算符负责缩小转换,而无需显式转换它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.