看着 GenericWhitespaceCheck在 Checkstyle 文档中,
Left angle bracket (
<
):
- should be preceded with whitespace only in generic methods definitions.
- should not be preceded with whitespace when it is precede method name or following type name.
- should not be followed with whitespace in all cases.
Right angle bracket (
>
):
- should not be preceded with whitespace in all cases.
- should be followed with whitespace in almost all cases, except diamond operators and when preceding method name.
我不确定我是否完全理解背后的原因
<
后面不应该跟一个空格以及为什么 >
不应在 1 之前。换句话说,为什么是
Map<String>
session 结束 Map < String >
?这仅仅是因为随着参数数量和深度的增加,没有空格的版本更具可读性。
喜欢,
Map<String, List<String>>
比 Map < String, List < String > >
更具可读性?另外作为一个一般问题,是否有一些存储库/指南可以解释 Checkstyle 约定背后的原因?
最佳答案
简介an early tutorial on generics (从 2004 年开始)说(强调我的):
This tutorial is aimed at introducing you to generics. You may be familiar with similar constructs from other languages, most notably C++ templates. If so, you’ll soon see that there are both similarities and important differences. If you are not familiar with look-a-alike constructs from elsewhere, all the better; you can start afresh, without unlearning any misconceptions.
这是承认 Java 泛型看起来像 C++ 模板。 C++ 模板通常也会省略开头后面的空格
<
, 以及收盘前 >
.围绕 Java 泛型的约定将遵循它们在早期教程中的编写方式。
关于java - Java中通用括号之间没有空格的推理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59503986/