I have Collection<Number>
but I needed Collection<Integer>
How can I convert it safety?
And more common issue:
How to convert Collection<Parent>
to Collection<Child>
You can try this
Collection<Number> numbers = ...
Collection<Integer> integers = numbers.getClass().newInstance();
for(Number n : numbers) {
integers.add(n == null ? null : n.intValue());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With