DEMO code of java queue, priority queue, and two-way queue

Source: Internet
Author: User

DEMO code of java queue, priority queue, and two-way queue

Package org. rui. collection2.queues; import java. util. using list; import java. util. priorityQueue; import java. util. queue; import java. util. concurrent. arrayBlockingQueue; import java. util. concurrent. concurrent1_queue; import java. util. concurrent. linkedBlockingQueue; import java. util. concurrent. priorityBlockingQueue; import org. rui. generics. anonymity. generator;/*** The following is a basic example of most operations related to the Queue implementation. * In addition to the priority Queue, queue will precisely generate the elements in the order in which they are placed * @ author lenovo **/public class QueueBeHavior {private static int count = 10; static
 
  
Void test (Queue
  
   
Queue, Generator
   
    
Gen) {for (int I = 0; I
    
     
{String [] s = ("one tow three four five six seven eight nine ten ". split (""); int I; public String next () {return s [I ++] ;}} public static void main (String [] args) {test (new partition list
     
      
(), New Gen (); test (new PriorityQueue
      
        (), New Gen (); test (new ArrayBlockingQueue
       
         (Count), new Gen (); test (new concurrent1_queue
        
          (), New Gen (); test (new LinkedBlockingQueue
         
           (), New Gen (); test (new PriorityBlockingQueue
          
            (), New Gen () ;}}/** output: one pair three four five six seven eight nine ten eight five four nine one seven six ten three tow one tow three four five six seven eight nine ten one pair three four five six seven eight nten one tow three four five six seven eight nine ten eight five four nine one seven six ten three tow */
          
         
        
       
      
     
    
   
  
 

Package org. rui. collection2.queues; import java. util. priorityQueue; /* ** priority queue * primary and secondary priority sorting * the sorting order of this list is also controlled by Comparable * @ author lenovo **/public class ToDoList extends PriorityQueue
 
  
{Static class ToDoItem implements Comparable
  
   
{Private char primary; // main private int secondary; // two private String items; public ToDoItem (String item, char primary, int secondary) {this. primary = primary; this. secondary = secondary; this. item = item;} public int compareTo (ToDoItem o) {if (primary> o. primary) // first compare the main return + 1; if (primary = o. primary) if (secondary> o. secondary) // return + 1; else if (secondary = o. secondary) return 0; return-1;} public String ToString () {return Character. toString (primary) + secondary + ":" + item;} public void add (String td, char pri, int sec) {super. add (new ToDoItem (td, pri, sec);} public static void main (String [] args) {ToDoList to = new ToDoList ();. add ("Empty Trash", 'C', 4);. add ("Feed dog", 'A', 2);. add ("Feed bird", 'B', 7);. add ("Mow lawn", 'C', 3);. add ("Water lawn", 'A', 1);. add ("Feed cat", 'B', 1); while (! To. isEmpty () {System. out. println (. remove () ;}}/ ** output: A1: Water lawnA2: Feed dogB1: Feed catB7: Feed birdC3: Mow lawnC4: Empty Trash */
  
 

Package org. rui. collection2.queues; import java. util. queue list;/*** a two-way queue is a queue, but you can add or remove elements at any end. * This interface cannot be implemented by queue list, however, you can use a combination to create a Deque class, * @ author lenovo ** @ param
 
  
*/Public class Deque
  
   
{Private domains list
   
    
Deque = new consumer list
    
     
(); Public void addFirst (T e) {deque. addFirst (e);} public void addLast (T e) {deque. addLast (e);} public T getFirst (T e) {return deque. getFirst ();} public T getLast (T e) {return deque. getLast ();} public T removeFirst () {return deque. removeFirst ();} public T removeLast () {return deque. removeLast ();} public int size () {return deque. size ();} public String toString () {return deque. toString ();} // and other methods as necessary ............ //////////////////////////////////////// /////// public static void fillTest (Deque
     
      
De) {for (int I = 10; I <17; I ++) de. addFirst (I); for (int I = 50; I <55; I ++) de. addLast (I);} public static void main (String [] args) {Deque
      
        Deque = new Deque
       
         (); FillTest (deque); System. out. println (deque); while (deque. size ()! = 0) System. out. print (deque. removeFirst () + ""); System. out. println (); fillTest (deque); while (deque. size ()! = 0) System. out. print (deque. removeLast () + ""); System. out. println () ;}/ ** output: [16, 15, 14, 13, 12, 11, 10, 50, 51, 52, 53, 54] 16 15 14 13 12 11 10 50 51 52 53 54 53 52 51 50 10 11 12 13 14 15 16 */
       
      
     
    
   
  
 

Related Article

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.