简体   繁体   English

使用Java流将字母从A打印到Z.

[英]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: 这更好,因为:

  1. No boxing overhead thus more efficient 没有拳击开销因此更有效率
  2. if you were to stop at say letter 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的上限,因为你需要找到截断无限流的数字。
  3. Along with the boxing 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. 以下是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是一个Characteri + 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.

相关问题 Java中文本文件中字母(az)的相对频率计数 - Relative Frequency Count of letters (a-z) from a text file in Java Java-如何在不使用!@#$%^&*()的情况下从0打印到z?&gt; &lt;“:{} |字母,仅包括字母和数字 - Java - How to print from 0 to z without !@#$%^&*()?><":{}| letters, only alphabet and numbers Java 8 DateTimeFormatter:与“z”和“Z”模式字母混淆 - Java 8 DateTimeFormatter: confusion with 'z' and 'Z ' pattern letters 使用 java 中的 stream 按顺序打印列表的数据 - printing the data of list in the order using stream in java 从数组中选择的项目中打印出特定的字母(Java) - printing out specific letters from chosen items from an array (Java) Java:如何检查来自az的随机字母,至少10个字母中的2个字母应该是元音 - Java: How to check the random letters from a-z, out of 10 letters minimum 2 letter should be a vowels 使用 java 8 stream 将字符串中单词的首字母大写 - Capitalize first letters in words in the string with different separators using java 8 stream 如何转换地图<X, Map<Y,Z> &gt; 到地图<Y, Map<X,Z> &gt; 使用 Java8 流 - How to convert Map<X, Map<Y,Z>> to Map<Y, Map<X,Z>> using Java8 stream Java Print Stream打印空间 - Java Print Stream printing spaces Java stream 中的计数和打印值 - Counting AND printing values in Java stream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM