Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently delete all elements from ListBuffer in Scala?

Tags:

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?

like image 616
Yevgen Avatar asked Oct 08 '15 16:10

Yevgen


1 Answers

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()
like image 92
Brian Avatar answered Oct 29 '22 08:10

Brian