I want the first n elements of some List
. From what I can tell, I have two options: take(n)
and getRange(0, n)
.
A very commonly used collection in programming is an array. Dart represents arrays in the form of List objects. A List is simply an ordered group of objects.
There are broadly two types of lists on the basis of their length: Fixed Length List. Growable List.
List, Set, Queue are iterable while Maps are not. Iterable collections can be changed i.e. their items can be modified, add, remove, can be accessed sequentially. The map doesn't extend iterable.
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.
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