ArrayDeque

import java.util.Deque;
import java.util.ArrayDeque;

public class ArrayDequeExample {

   public static void main(String[] args) {
		  
       /*
	* We cannot create instance of a Deque as it is an
	* interface, we can create instance of ArrayDeque or
	* LinkedList and assign it to Deque
	*/
       Deque<String> dq = new ArrayDeque<String>();
	    
       /*
	* Adding elements to the Deque.
	* addFirst() adds element at the beginning 
        * and addLast() method adds at the end.
	*/
	dq.add("Glenn");
	dq.add("Negan");
	dq.addLast("Maggie");
	dq.addFirst("Rick");
	dq.add("Daryl");
	    
	System.out.println("Elements in Deque:"+dq);

        /*
	 * We can remove element from Deque using remove() method,
	 * we can use normal remove() method which removes first 
	 * element or we can use removeFirst() and removeLast()
	 * methods to remove first and last element respectively.
	 */
	System.out.println("Removed element: "+dq.removeLast());
	    
	/*
	 * element() method - returns the head of the
	 * Deque. Head is the first element of Deque
	 */
	 System.out.println("Head: "+dq.element());
	    
	/*
	 * pollLast() method - this method removes and returns the 
	 * tail of the Deque(last element). Returns null if the Deque is empty.
	 * We can also use poll() or pollFirst() to remove the first element of
	 * Deque.
	 */
	System.out.println("poll(): "+dq.pollLast());
	    
	/*
	 * peek() method - it works same as element() method,
	 * however it returns null if the Deque is empty. We can also use 
	 * peekFirst() and peekLast() to retrieve first and last element
	 */
	System.out.println("peek(): "+dq.peek());
	    
	//Again printing the elements of Deque
	System.out.println("Elements in Deque:"+dq);
   }
}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source