How do I instantiate a Queue object in java?

后端 未结 8 704
南旧
南旧 2020-11-29 16:39

When I try:

Queue q = new Queue();

the compiler is giving me an error. Any help?

Also, if I want to i

相关标签:
8条回答
  • 2020-11-29 16:54

    A Queue is an interface, which means you cannot construct a Queue directly.

    The best option is to construct off a class that already implements the Queue interface, like one of the following: AbstractQueue, ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingQueue, LinkedList, PriorityBlockingQueue, PriorityQueue, or SynchronousQueue.

    An alternative is to write your own class which implements the necessary Queue interface. It is not needed except in those rare cases where you wish to do something special while providing the rest of your program with a Queue.

    public class MyQueue<T extends Tree> implements Queue<T> {
       public T element() {
         ... your code to return an element goes here ...
       }
    
       public boolean offer(T element) {
         ... your code to accept a submission offer goes here ...
       }
    
       ... etc ...
    }
    

    An even less used alternative is to construct an anonymous class that implements Queue. You probably don't want to do this, but it's listed as an option for the sake of covering all the bases.

    new Queue<Tree>() {
       public Tree element() {
         ...
       };
    
       public boolean offer(Tree element) {
         ...
       };
       ...
    };
    
    0 讨论(0)
  • 2020-11-29 16:55

    Queue is an interface in java, you could not do that. try:

    Queue<Integer> Q = new LinkedList<Integer>();
    
    0 讨论(0)
  • 2020-11-29 16:56

    Queue is an interface in java, you can not do that.

    Instead you have two options:

    option1:

    Queue<Integer> Q = new LinkedList<>();
    

    option2:

    Queue<Integer> Q = new ArrayDeque<>();
    

    I recommend using option2 as it is bit faster than the other

    0 讨论(0)
  • 2020-11-29 17:00
    Queue<String> qe=new LinkedList<String>();
    
    qe.add("b");
    qe.add("a");
    qe.add("c");
    

    Since Queue is an interface, you can't create an instance of it as you illustrated

    0 讨论(0)
  • 2020-11-29 17:06

    Queue in Java is defined as an interface and many ready-to-use implementation is present as part of JDK release. Here are some: LinkedList, Priority Queue, ArrayBlockingQueue, ConcurrentLinkedQueue, Linked Transfer Queue, Synchronous Queue etc.

    SO You can create any of these class and hold it as Queue reference. for example

    import java.util.LinkedList;
    import java.util.Queue;
    
    public class QueueExample {
    
     public static void main (String[] args) {
      Queue que = new LinkedList();
      que.add("first");
      que.offer("second");
      que.offer("third");
      System.out.println("Queue Print:: " + que);
      
      String head = que.element();
      System.out.println("Head element:: " + head);
      
      String element1 = que.poll();
      System.out.println("Removed Element:: " + element1);
      
      System.out.println("Queue Print after poll:: " + que);
      String element2 = que.remove();
      System.out.println("Removed Element:: " + element2);
      
      System.out.println("Queue Print after remove:: " + que);  
     }
    }
    

    You can also implement your own custom Queue implementing Queue interface.

    0 讨论(0)
  • 2020-11-29 17:07

    Queue is an interface. You can't instantiate an interface directly except via an anonymous inner class. Typically this isn't what you want to do for a collection. Instead, choose an existing implementation. For example:

    Queue<Integer> q = new LinkedList<Integer>();
    

    or

    Queue<Integer> q = new ArrayDeque<Integer>();
    

    Typically you pick a collection implementation by the performance and concurrency characteristics you're interested in.

    0 讨论(0)
提交回复
热议问题