Java Collections.checked*() 与通用集合

标签 java generics collections

<分区>

Java 的 Collections.checked*() api 为我们提供了底层集合的类型安全 View 。但是检查发生在运行时并抛出运行时异常,这可能会影响性能。通过使用泛型集合为这些集合赋予特定类型,可以在编译时强制执行相同的类型检查。那么,在某些情况下,Collections.checked*() 是否比指定类型的泛型集合得分更高?

最佳答案

javadoc 解释得很好:

http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#checkedCollection%28java.util.Collection,%20java.lang.Class%29

The generics mechanism in the language provides compile-time (static) type checking, but it is possible to defeat this mechanism with unchecked casts. Usually this is not a problem, as the compiler issues warnings on all such unchecked operations. There are, however, times when static type checking alone is not sufficient. For example, suppose a collection is passed to a third-party library and it is imperative that the library code not corrupt the collection by inserting an element of the wrong type.

关于Java Collections.checked*() 与通用集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7011486/

相关文章:

java - 了解 Java 集合类中泛型的使用

java - Spring MVC Apache Oltu 和 Salesforce 集成示例 OAUTH_APPROVAL_ERROR_GENERIC : An unexpected error has occured during authentication

java - Spring Boot JPA - 未设置 'hibernate.dialect' 时,对 DialectResolutionInfo 的访问不能为空

java - Reactor Mono,如何同步进程(使用 .map())并从 response.setComplete() 返回 Mono<void>

Java generics - 通配符的目的除了下限?

java - 使用集合按顺序存储待处理的对象

java - 为什么我不能使用带有通配符的多个类型参数?

java - 如何添加到通配符 Collection<?>?

Java 集合在提供引用的同时确保唯一性

java - 从两个列表中删除重复项