开发者

What is the most efficient way to create empty ListBuffer?

开发者 https://www.devze.com 2022-12-26 11:29 出处:网络
What is the most efficient way to create empty ListBuffer ? val l1 = new mutable.ListBuffer[String] val l2 = mutable.ListBuffer[String] ()

What is the most efficient way to create empty ListBuffer ?

  1. val l1 = new mutable.ListBuffer[String]
  2. val l2 = mutable.ListBuffer[String] ()
  3. val l3 = mut开发者_StackOverflow中文版able.ListBuffer.empty[String]

There are any pros and cons in difference?


Order by efficient:

  1. new mutable.ListBuffer[String]
  2. mutable.ListBuffer.empty[String]
  3. mutable.ListBuffer[String] ()

You can see the source code of ListBuffer & GenericCompanion


new mutable.ListBuffer[String] creates only one object (the list buffer itself) so it should be the most efficient way. mutable.ListBuffer[String] () and mutable.ListBuffer.empty[String] both create an instanceof scala.collection.mutable.AddingBuilder first, which is then asked for a new instance of ListBuffer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号