I tought that List is enough but I need to add element to my list.
I've tried to put this in ListBuffer constructor but without result.
var leavesValues: ListBuffer[Double] =
leaves
.collect { case leaf: Leaf => leaf.value.toDouble }
.toList
Later on I'm going to add value to my list so my expected output is mutable list.
But what if I need to append single value to the end of leavesValues
- I can reverse but it's not good enough
I can use ListBuffer like below but I believe that there is cleaner solution:
val leavesValues: ListBuffer[Double] = ListBuffer() leavesValues.appendAll(leaves .collect { case leaf: Leaf => leaf.value.toDouble } .toList)
you can do it like this after getting the list of leavesValues you can prepand the value you want to add into the list.