If I have a List
of BigDecimal
objects, is it possible to typecast the whole list to a List
of Long
values without having to iterate over every BigDecimal
object?
You will need to iterate one way or another. If you want to "hide" the iteration, you can use a stream:
List<Long> longs = bigs.stream().map(BigDecimal::longValue).collect(Collectors.toList());
But there will still be an iteration in the background.
You mention that you don't want to iterate twice - you could save the stream of longs for later use:
LongStream longs = bigs.stream().mapToLong(BigDecimal::longValue);
And apply additional operations on that stream before collecting the results.