B3th4 B3th4 - 1 year ago 86
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 Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download