我能Class<T>来Class<T[]>吗?
public static <T> void doSometing(final Class<T[]> arrayType) {
final Class<T> elementType; // = @@?
}
Run Code Online (Sandbox Code Playgroud)
或者,我可以Class<T[]>从中获得Class<T>吗?
需求
Map<String, String>?a=b&c=d方法1
TreeMap.方法2
ArrayList预测大小(source-map.size*2)方法3
LinkedList哪种方法最好?
这可能吗?
class MyService<T> {
@MyAnnotation(T.class)
private String myProperty;
}
Run Code Online (Sandbox Code Playgroud)
编译器不喜欢它.我怎样才能做到这一点?
UPDATE
我发现我做不到.请参阅错误设置注释值为常量的Class <?>,为什么?.
我建议限制应该预先声明a.length / 2.一个人告诉他,他相信编译器无论如何都会增强它
所以我试过了.
public class Loop1 {
public static void main(final String[] args) {
final String[] a = {};
for (int i = 0; i < a.length / 2; i++) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
public class Loop2 {
public static void main(final String[] args) {
final String[] a = {};
final int l = a.length / 2;
for (int i = 0; i < l; i++) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我打印这些课程时, …
我有一个可为空List的BufferedImage秒.
List<BufferedImage> list = null; // or not null.
Run Code Online (Sandbox Code Playgroud)
我发现我可以像这样刷新列表中的每个图像.
Optional.ofNullable(list)
.ifPresent(l -> l.forEach(i -> i.flush())); // ok
Run Code Online (Sandbox Code Playgroud)
我可以做到这一点.
Optional.ofNullable(list)
.ifPresent(l -> l.forEach(BufferedImage::flush)); // ok
Run Code Online (Sandbox Code Playgroud)
为什么编译器在我尝试这样做时会抱怨?
Optional.ofNullable(list)
.ifPresent(List::forEach(BufferedImage::flush)); // not ok
Run Code Online (Sandbox Code Playgroud) int main(int argc, char * argv[]) {
int a = 10;
int * sp = &a;
int * * dp1 = &sp;
int * * dp2 = &&a; // NG
int * * dp3 = &(&a); // NG
int * * dp4 = &((int *) &a); // NG
}
Run Code Online (Sandbox Code Playgroud)
$ cc test.c
test.c: In function ‘main’:
test.c:6:17: error: lvalue required as unary ‘&’ operand
int * * dp3 = &(&a); // NG
^
test.c:7:17: error: lvalue required as unary ‘&’ …Run Code Online (Sandbox Code Playgroud) 我有一个枚举,我想我可以缓存values().
enum MyEnum {
SOME;
private static volatile Set<MyEnum> values_;
public static Set<MyEnum> values_() {
Set<MyEnum> result = values_;
if (result == null) {
values_ = result = Collections.unmodifiableSet(EnumSet.allOf(MyEnum.class));
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了允许重复初始化的延迟初始化。
奏鸣曲也有volatile Set<MyEnum>部分抱怨。
SonaLint:使用线程安全类型;添加“易失性”不足以使该字段成为线程安全的。
https://rules.sonarsource.com/java/RSPEC-3077
我的代码有问题吗?