I am trying to combine the elements of two ListBuffers into one single ListBuffer. For standard ListBuffers I know you can use ++ to accomplish this, however the the ListBuffer contains a class as its element.
For example:
class ExampleBuffer {
var exBuff = new ListBuffer[Example]
}
class Example (
var name: String = null,
var age: Option[Int] = None,
var yob: Option[Int] = None,
var gender: Option[String] = None
)
val e1 = new Example
val e2 = new Example
e1.name = "Jack"
e1.age = Some(25)
e2.yob = Some(1990)
e2.gender = Some("m")
val b1 = ListBuffer(e1)
val b2 = ListBuffer(e2)
val b3 = b1 ++ b2
So:
b1 = scala.collection.mutable.ListBuffer[Example] = ListBuffer(Example@4a60ee36)
b2 = scala.collection.mutable.ListBuffer[Example] = ListBuffer(Example@4d33940d)
b3 = scala.collection.mutable.ListBuffer[Example] = ListBuffer(Example@4a60ee36, Example@4d33940d)
I would like to combine the two to contain all four elements and then pull from this buffer later. The issue is that when I try to concatenate the ListBuffers, I am left with ListBuffer(Example@4a60ee36, Example@4d33940d), rather than combining the actual elements of class Example1 and Example2.
Is there a way that I can extract the elements from each ListBuffer to add each element to a new ListBuffer? Pulling each element individually though b1(0).name, etc into a new ListBuffer will not be a great method since the real issue I am working with is much larger than the example above.