25

I'm trying to instantiate an object of Queue using below code

var queue: Queue<Int> = Queue()

But I get this

Interface Queue does not have constructors

No idea what's going on, while searching I found this link.

But I don't understand anything. Please help.

Vicky
  • 1,807
  • 5
  • 23
  • 34

3 Answers3

35

Queue is an interface. So you can't instantiate an interface, you have to implement it or instantiate a class that implement it.

For example, you can do var queue: Queue<Int> = ArrayDeque<Int>(). ArrayDeque implements Queue.

Kevin Robatel
  • 8,025
  • 3
  • 44
  • 57
  • This will be then a deque not queue I guess? – stephanmg Feb 18 '20 at 23:03
  • `Deque` extends `Queue` – abitcode Mar 20 '20 at 23:15
  • Hmm, after going through multiple stackoverflow questions, what I figured out is there are various implementations of the Queue interface. Now my problem is which one should I use. I just want a really basic queue. Should I go with ArrayDeque or LinkedList? – capt.swag Dec 26 '20 at 14:18
  • `ArrayDeque` is the choice to go for in doubt, because LinkedLists carry significant overhead that is only justified in rare cases: https://stackoverflow.com/questions/6163166/why-is-arraydeque-better-than-linkedlist/6163204#6163204 – xeruf Aug 30 '23 at 09:09
13

You trying to create instance of interface but don`t override methods for it. You should use something like this:

val queueA = LinkedList<Int>()
val queueB = PriorityQueue<Int>()

Also you can read more about queue implementations here

Silvestr
  • 704
  • 4
  • 13
3

Queue is an interface. So you can't instantiate an interface. You should use something like this:

val queue: Queue<Int> = LinkedList()
Pavan Bilagi
  • 1,618
  • 1
  • 18
  • 23