Yevgen
Yevgen

Reputation: 4709

How to efficiently delete all elements from ListBuffer in Scala?

I have a ListBuffer with thousand elements. After program has done calculations I want to fill it with new data. Is there a way like in C with free() to empty it? Or is it a good way to assign null to my ListBuffer and garbage collector will do all the work?

Upvotes: 9

Views: 7548

Answers (1)

Brian
Brian

Reputation: 20285

The method clear does just that.

scala> val xs = scala.collection.mutable.ListBuffer(1,2,3,4,5)
xs: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 5)

scala> xs.clear()

scala> xs
res2: scala.collection.mutable.ListBuffer[Int] = ListBuffer()

Upvotes: 21

Related Questions