What is the difference between size and length of a Seq? When to use one and when the other?
scala> var a :Seq[String] = Seq("one", "two") a: Seq[String] = List(one, two) scala> a.size res6: Int = 2 scala> a.length res7: Int = 2 It's the same?
Thanks
Advertisements. Scala Seq is a trait to represent immutable sequences. This structure provides index based access and various utility methods to find elements, their occurences and subsequences. A Seq maintains the insertion order.
A Seq is an Iterable that has a defined order of elements. Sequences provide a method apply() for indexing, ranging from 0 up to the length of the sequence. Seq has many subclasses including Queue, Range, List, Stack, and LinkedList. A List is a Seq that is implemented as an immutable linked list.
Definitions: Sequence in Scala is a collection that stores elements in a fixed order.
It depends on the collection you were using in the first place. If you had a list you'll get your order. If on the other hand you had a set, then probably not.
Nothing. In the Seq doc, at the size method it is clearly stated: "The size of this sequence, equivalent to length.".
size is defined in GenTraversableOnce, whereas length is defined in GenSeqLike, so length only exists for Seqs, whereas size exists for all Traversables. For Seqs, however, as was already pointed out, size simply delegates to length (which probably means that, after inlining, you will get identical bytecode).
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