Double-ended queues in Java and their use

Source: Internet
Author: User

Go directly to the code.

 Packagecollections;ImportJava.util.Deque;Importjava.util.LinkedList;/*** @Package Collections * @date 2017-11-28 PM 5:53:32*/ Public classDequetest {/**     * @paramargs*/     Public Static voidMain (string[] args) {Deque<String> deque =NewLinkedlist<string>(); Deque.add ("D"); Deque.add (E); Deque.add ("F"); //Remove element from first team, not deleteSYSTEM.OUT.PRINTLN ("Team first Take out element:" +Deque.peek ()); System.out.println ("Queue is:" +deque); //add elements from the first team (queue with capacity limit, none with AddFirst)Deque.offerfirst ("C"); System.out.println ("When the team first joins the element:" +deque); //add elements from the end of the queue (queues have capacity constraints, none are used AddLast)Deque.offerlast ("G"); System.out.println ("After adding elements to the end of the team:" +deque); //Team Tail add elementDeque.offer ("H"); System.out.println ("After adding elements to the end of the team:" +deque); //gets and removes the first element of the queue, Pollfirst () is also, except that the Removefirst throws a Nosuchelementexception exception when the queue is empty, which returns nullDeque.removefirst (); System.out.println ("Gets and removes the first element of a queue after:" +deque); //gets and removes the first element of the queue, the only difference between this method and Polllast is that when the queue is empty, Removelast throws a Nosuchelementexception exception, which returns nullDeque.removelast (); System.out.println ("Gets and removes the last element of the queue after:" +deque); //gets the first element of the queue. The only difference between this method and Peekfirst is that if this double-ended queue is empty, it throws nosuchelementexception, which returns nullSystem.out.println ("Get queue first element is:" +Deque.getfirst ()); System.out.println ("Gets the first element of the queue after:" +deque); //gets the last element of the queue. The only difference between this method and Peeklast is that if this double-ended queue is empty, it throws nosuchelementexception, which returns nullSystem.out.println ("Get queue last element is:" +deque.getlast ()); System.out.println ("Gets the first element of the queue after:" +deque); //Loop get elements and remove elements from queue         while(Deque.size () >0) {System.out.println ("Get element is:" + deque.pop () + "and delete"); } System.out.println ("Queue is:" +deque); }}

The results are as follows:

First team take out elements: D queue for: [D, E, F] The first element of the team is: [C, D, E, F] After adding elements: [C, D, E, F, G] at the end of the team: [C, D, E, F, G, H] gets and removes the first element of the queue: [D, E, F, G, H ] Gets and removes the last element of the queue after: [D, E, F, G] Gets the queue the first element is: D gets the queue after the first element is: [D, E, F, G] Gets the queue the last element is: G Gets the queue after the first element is: [D, E, F, g] gets the element: D and removes the Get element as: E and remove the Get element as: F and remove the get element as: G and delete the queue as: []

Double-ended queues in Java and their use

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.