我有Collection<Number>
但我需要 Collection<Integer>
我怎样才能安全地转换它?
还有更常见的问题:
如何转换Collection<Parent>
至 Collection<Child>
最佳答案
你可以试试这个
Collection<Number> numbers = ...
Collection<Integer> integers = numbers.getClass().newInstance();
for(Number n : numbers) {
integers.add(n == null ? null : n.intValue());
}
关于java - 如何将 Collection<Number> 转换为 Collection<Integer> 安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20758080/