I want the first n elements of some List
. From what I can tell, I have two options: take(n)
and getRange(0, n)
.
The most obvious difference is that take()
can only use elements at the beginning, you can combine it with skip()
like list.skip(3).take(5)
to get similar behavior though.
list.take()
is lazy evaluated which works nice with functional style programming and might be more efficient if the elements aren't actually iterated over later.
list.take()
also is tolerant when there aren't as many elements in the list as requested. take()
returns as many as available, getRange()
throws.
take()
is available on all iterables (also streams), getRange()
is only available by default on list.