Scala - Seq 的大小和长度有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22966705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:10:45  来源:igfitidea点击:

Scala - What is the difference between size and length of a Seq?

scalascala-2.10seq

提问by YoBre

What is the difference between size and length of a Seq? When to use one and when the other?

Seq 的大小和长度有什么区别?什么时候用一个,什么时候用另一个?

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

谢谢

采纳答案by Peter

Nothing. In the Seq doc, at the size method it is clearly stated: "The size of this sequence, equivalent to length.".

没有什么。在Seq doc 中,在 size 方法中明确指出:“此序列的大小,相当于length.”。

回答by J?rg W Mittag

sizeis defined in GenTraversableOnce, whereas lengthis defined in GenSeqLike, so lengthonly exists for Seqs, whereas sizeexists for all Traversables. For Seqs, however, as was already pointed out, sizesimply delegates to length(which probably means that, after inlining, you will get identical bytecode).

size在 中定义GenTraversableOnce,而length在 中定义GenSeqLike,所以length只存在于Seqs,而size存在于所有Traversables。Seq但是,正如已经指出的那样,对于s,size只需将length其委托给(这可能意味着,在内联之后,您将获得相同的字节码)。

回答by Mark Tickner

In a Seq they are the same, as others have mentioned. However, for information, this is what IntelliJ warns on a scala.Array:

在 Seq 中,它们是相同的,正如其他人提到的那样。但是,对于信息,这是 IntelliJ 警告的内容scala.Array

Replace .size with .length on arrays and strings

Inspection info: This inspection reports array.size and string.size calls. While such calls are legitimate, they require an additional implicit conversion to SeqLike to be made. A common use case would be calling length on arrays and strings may provide significant advantages.

在数组和字符串上用 .length 替换 .size

检查信息:此检查报告 array.size 和 string.size 调用。虽然此类调用是合法的,但它们需要进行额外的到 SeqLike 的隐式转换。一个常见的用例是在数组和字符串上调用 length 可能提供显着的优势。

回答by monkHyman

Nothing, one delegates to the other. See SeqLike trait.

没什么,一个委托给另一个。参见 SeqLike 特征。

  /** The size of this $coll, equivalent to `length`.
   *
   *  $willNotTerminateInf
   */
  override def size = length

回答by user11595225

I did an experiment, using Scala version 2.12.8, and a million item list. Upon the first use, length() is 7 or 8 times faster than size(). But on the 2nd try on the same list, size() is about the same speed as length().

我做了一个实验,使用 Scala 版本 2.12.8,和一百万个项目列表。第一次使用时,length() 比 size() 快 7 或 8 倍。但是在第二次尝试相同的列表时,size() 与 length() 的速度大致相同。

However, after some time, presumably the cache is gone, size() is slow() by 7 or 8 times again.

但是,一段时间后,大概缓存消失了,size() 又慢了 7 或 8 倍。

This shows that length() is preferred for sequences. It's not just another name for size().

这表明 length() 是序列的首选。它不仅仅是 size() 的另一个名称。