Methods for parsing queues in Java and simulating queues with LinkedList collections _java

Source: Internet
Author: User

Description of queues in the API:

Public interface queue<e>
extends collection<e>

The collection that is used to save elements before the element is processed. In addition to the basic Collection operations, queues provide additional inserts, fetches, and checks. There are two forms of each method: one throws an exception (when the operation fails) and the other Returns a special value (null or FALSE, depending on the operation). The latter form of the insert operation is designed specifically for a capacity-constrained Queue implementation, and in most implementations the insert operation does not fail.

Queues usually (but not necessarily) sort the elements in FIFO (first-in first Out) way. However, the priority queue and the LIFO queue (or stack) are exceptions, which sort the elements according to the natural order of the comparator or element provided, which sorts the elements in terms of LIFO (LIFO). Regardless of the sort method used, the header of the queue is the element that is removed by calling remove () or poll (). In the FIFO queue, all new elements are inserted at the end of the queue. Other kinds of queues may use different element placement rules. Each QUEUE implementation must specify its order attributes.
If possible, the Offer method can insert an element, otherwise it returns false. Unlike the Collection.add method, this method can only fail to add an element by throwing an unchecked exception. The Offer method is designed for normal failure situations, not exceptions, such as in a fixed (bounded) queue.
The Remove () and poll () methods can remove and return the header of the queue. Exactly which element is removed from the queue is the function of the queue sorting policy, and the policy is different in various implementations. The Remove () and poll () methods behave differently only when the queue is empty: The Remove () method throws an exception, and the poll () method returns NULL.
Element () and peek () are returned, but not removed, the header of the queue.
The queue interface does not define a method for blocking queues, which is common in concurrent programming. The Blockingqueue interface defines methods that wait for elements to appear or have free space in the queue, and these methods extend this interface.
The Queue implementation typically does not allow the insertion of NULL elements, although some implementations, such as LinkedList, do not prohibit the insertion of NULL. Even in a null-enabled implementation, NULL should not be inserted into the queue because Null is also used as a special return value for the poll method, indicating that the queue does not contain elements.
The queue implementation typically does not define an element-based version of the Equals and hashcode methods, but rather inherits the identity-based version from the Object class, because element equality is not always well-defined for queues with the same elements but with different sort attributes.

Using queues in Java can be simulated with linkedlist collections


Method
Use the LinkedList collection and use the AddLast, Removefirst, and IsEmpty APIs to simulate queue operations collectively

into queues

  void AddLast (e e); Inserts an element at the end of this list 

Out of the queue

  E Removefirst (); To remove and return the first element of a list 

Short sentence

  Boolean IsEmpty (); To determine whether a queue is empty  

Sample code

Package corejavaone; 
   
  Import java.util.LinkedList; 
  Import java.util.NoSuchElementException; 
   
  public class Simulatequeue { 
    private linkedlist<integer> queue = new linkedlist<integer> (); 
   
    public Boolean IsEmpty () {return 
      this.queue.isEmpty (); 
    } 
   
    public void EnQueue (int data) { 
      this.queue.addLast (data); 
    } 
   
    public int dequeue () throws Nosuchelementexception {return 
      this.queue.removeFirst (); 
    } 
   
    public static void Main (string[] args) { 
      Simulatequeue q = new Simulatequeue (); 
   
      Q.enqueue (1); 
      Q.enqueue (2); 
      Q.enqueue (3); 
   
      while (! Q.isempty ()) { 
        int data = Q.dequeue (); 
        SYSTEM.OUT.PRINTLN (data);}} 
   

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.