6

Does Scala have Double Side Queue similar to Java Deque or Python deque?

I see only Stack and Queue in Scala 2.12 API but just want to double check.

CSY
  • 543
  • 1
  • 6
  • 11
  • 3
    What about the Java `Deque`? Why doesn't that work? – Boris the Spider Apr 02 '18 at 20:56
  • There used to be `mutable.DoubleLinkedList`, but it was deemed too awkward and not useful enough to maintain, so it is now deprecated for some reason. Could you describe the actual problem more precisely, what do you need the deque for? – Andrey Tyukin Apr 02 '18 at 21:09
  • thanks. I will just use the Java Deque then. – CSY Apr 04 '18 at 00:48

2 Answers2

2

You can use Vector.drop or Vector.dropRight

val v = Vector(1,2,3)
v :+ 4 // Vector(1, 2, 3, 4)
0 +: v // Vector(0, 1, 2, 3)
v.drop(1) // Vector(2, 3)
v.dropRight(1) // Vector(1, 2)
Józef Podlecki
  • 10,453
  • 5
  • 24
  • 50
shuvomiah
  • 410
  • 2
  • 9
  • 2
    this is O(log(n)) complexity because Vector is a tree. double-sided queue like ArrayDeque in scala 2.13 is O(1) constant time for adding to head/tail of the queue – alex Jan 28 '21 at 18:12