As a quick review,
Set<Object> is a parameterized type representing a set that can contain objects of
any type, Set<?> is a wildcard type representing a set that can contain only
objects of some unknown type, and Set is a raw type, which opts out of the
generic type system. The first two are safe and the last is not.
Set<Object> is a parameterized type representing a set that can contain objects of
any type, Set<?> is a wildcard type representing a set that can contain only
objects of some unknown type, and Set is a raw type, which opts out of the
generic type system. The first two are safe and the last is not.
If
you can’t eliminate a warning, and you can prove that the code that
provoked the warning is typesafe, then (and only then) suppress the warning
with an @SuppressWarnings("unchecked")
annotation.

本文介绍了Java中泛型的使用,包括参数化类型如Set<Object>、通配符类型如Set<?>以及原始类型Set的区别。强调了前两种类型的安全性,并建议在无法消除警告且能证明类型安全的情况下使用@SuppressWarnings(unchecked)注解来抑制警告。
363

被折叠的 条评论
为什么被折叠?



