Use Queue list in Java to implement Queue

Source: Internet
Author: User

Use Queue list in Java to implement Queue

The Queue list provides methods to support Queue behavior and implements the Queue interface. Therefore, the Queue list can be used as an implementation of Queue.

Package cn. usst. queue. demo; import java. util. using list; import java. util. queue; import java. util. random;/** basic use of Queue */public class QueueDemo {public static void main (String [] args) {Queue
 
  
Queue = new queue list
  
   
(); Random random = new Random (47); for (int I = 0; I <10; I ++) {queue. offer (random. nextInt (I + 10);} printQ (queue); Queue
   
    
Qc = new employee list
    
     
(); For (char c: "Brontosaurus ". toCharArray () {qc. offer (c);} printQ (qc);} private static void printQ (Queue queue) {while (queue. peek ()! = Null) {System. out. println (queue. remove () + "");} System. out. println () ;}}/** Offer () insert an element to the end of the Team * peek () and element () both return the head of the team * peek () the method returns null when the queue is empty. element () will throw the NoSuchElementException exception * the poll () and remove () methods will be removed and return the header * poll () if the queue is empty, null is returned, while remove () throws the NoSuchElementException exception */
    
   
  
 

PriorityQueue: implementation of priority queue

Package cn. usst. queue. demo; import java. util. arrays; import java. util. collections; import java. util. list; import java. util. priorityQueue; import java. util. queue; import java. util. random; public class PriorityQueueDemo {public static void main (String [] args) {PriorityQueue
 
  
PriorityQueue = new PriorityQueue
  
   
(); Random rand = new Random (); for (int I = 0; I <10; I ++) {priorityQueue. offer (rand. nextInt (I + 10);} printQ (priorityQueue); List
   
    
Ints = Arrays. asList (25, 22, 20, 18, 14, 9, 8, 2, 4, 7); priorityQueue = new PriorityQueue
    
     
(Ints); printQ (priorityQueue); // returns priorityQueue = new PriorityQueue.
     
      
(Ints. size (), Collections. reverseOrder (); priorityQueue. addAll (ints); printQ (priorityQueue);} private static void printQ (Queue queue) {while (queue. peek ()! = Null) {System. out. println (queue. remove () + "");} System. out. println ();}}
     
    
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.