scalalistbuffer

What is the most efficient way to create empty ListBuffer?


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 = mutable.ListBuffer.empty[String]

There are any pros and cons in difference?


Solution

  • 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