B3th4 B3th4 - 3 days ago 5
Scala Question

Scala Filter ArrayBuffer based on a Queue

I have an Arraybuffer which contains Int's. Beside that, I have a Queue that contains Int's too. I would like to remove from my Arraybuffer all the Int's that are also present in my queue. What is an efficient way to do this. Can I use the Filter function with something like:

newArray = Arraybuffer.filter(_ not in Queue)

Answer

I'd use a Set[A] for that, especially if your ArrayBuffer[Int] is large enough:

val queue = Queue[Int] = ???
val arrayBuffer: ArrayBuffer[Int] = ???

val set = queue.toSet

val filteredBuffer = arrayBuffer.filterNot(set.contains)
Comments