Java.util.ArrayDeque Class in Java | Set 2

Java.util.ArrayDeque Class in Java | Set 1
More Methods of util.ArrayDeque Class :

  1. offer(Element e) : java.util.ArrayDeque.offer(Element e) : inserts element at the end of deque.
    Syntax :

    public boolean offer(Element e)
    Parameters : 
    e - element to add
    Return : 
    true, if element is added else; false
    
  2. offerFirst(Element e) : java.util.ArrayDeque.offerFirst(Element e) : inserts element at the front of deque.
    Syntax :



    public boolean offerFirst(Element e)
    Parameters : 
    e - element to add
    Return : 
    true, if element is added else; false
    
  3. offerLast(Element e) : java.util.ArrayDeque.offerLast(Element e) : inserts element at the end of deque.
    Syntax :

    public boolean offerLast(Element e)
    Parameters : 
    e - element to add
    Return : 
    true, if element is added else; false
    
  4. peek() : java.util.ArrayDeque.peek(): returns head element without removing it.
    Syntax :

    public E peek()
    Parameters :
    --------
    Return : 
    head of deque or null if deque is empty.
    
  5. peekFirst() : java.util.ArrayDeque.peekFirst() :returns first element without removing it.
    Syntax :

    public E peekFirst()
    Parameters :
    --------
    Return : 
    first element of deque or null if deque is empty.
    
  6. peekLast() : java.util.ArrayDeque.peek(): returns last element without removing it.
    Syntax :

    public E peekLast()
    Parameters :
    --------
    Return : 
    Last element of deque or null if deque is empty.
    
  7. poll() : java.util.ArrayDeque.peek(): returns head element and also removes it
    Syntax :

    public E poll()
    Parameters :
    --------
    Return : 
    head of deque or null if deque is empty.
    
  8. pollFirst() : java.util.ArrayDeque.peek(): returns first element and also removes it
    Syntax :

    public E pollFirst()
    Parameters :
    --------
    Return : 
    first element of deque or null if deque is empty.
    
  9. pollLast() : java.util.ArrayDeque.peek(): returns last element and also removes it
    Syntax :

    public E pollLast()
    Parameters :
    --------
    Return : 
    last element of deque or null if deque is empty.
    
  10. pop() : java.util.ArrayDeque.pop() : pops out an element for stack repesentyed by deque
    Syntax :

    public E pop()
    Parameters :
    ---------
    Return : 
    element at front
    
  11. push(Element e) : java.util.ArrayDeque.push(Element e) : pushes an element onto stack repesentyed by deque
    Syntax :

    public void push(Element e)
    Parameters :
    e : element to be pushed
    Return : 
    ----------
    
  12. remove() : java.util.ArrayDeque.remove(): returns head element and also removes it
    Syntax :


    public E remove()
    Parameters :
    ------
    Return : 
    head of the deque
    
  13. removeFirst() : java.util.ArrayDeque.remove(): returns first element and also removes it
    Syntax :

    public E removeFirst()
    Parameters :
    ------
    Return : 
    first element of the deque
    
    
  14. removeLast() : java.util.ArrayDeque.remove(): returns last element and also removes it
    Syntax :

    public E removeLast()
    Parameters :
    ------
    Return : 
    last element of the deque
    
  15. removeFirstOccurrence(Obj) : java.util.ArrayDeque.removeFirstOccurrence(Obj) : removes the element where it first occur in the deque.
    Syntax :

    public boolean removeFirstOccurrence(Object obj)
    Parameters :
    obj : element to be removed
    Return : 
    true, if the element is removed; else False
    
  16. removeLastOccurrence(Obj) : java.util.ArrayDeque.removeLastOccurrence(Obj) : removes the element where it last occur in the deque.
    Syntax :

    public boolean removeLastOccurrence(Object obj)
    Parameters :
    obj : element to be removed
    Return : 
    true, if the element is removed; else False
    

Java Program explaining util.ArrayDeque class methods :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code explaining the use of ArrayDeque class methods
// offer(), offerFirst(), offerLast(), peek(), peekFirst(), peekLast()
// poll(), peekFirst(), peekLast(), pop(), push()
  
import java.util.*;
public class NewClass
{
    public static void main(String[] args)
    {
        // Intializing an deque
        Deque<Integer> d = new ArrayDeque<Integer>(10);
  
        // add() method to insert
        d.add(2);
        d.add(4);
  
        // offer() : add element at end
        d.offer(100);
        d.offer(101);
  
        // offerFirst() : add element at start
        d.offerFirst(1111);
        d.offerFirst(3333);
  
        // offerLast() : add element at end
        d.offerLast(5000);
        d.offerLast(50001);
  
        for (Integer element : d)
        {
            System.out.println("Element : " + element);
        }
  
        // peek() method : to get head
        System.out.println("\nHead element : " + d.peek());
  
        // peekFirst() method : to get First element
        System.out.println("First element : " + d.peekFirst());
  
        // peekLast() method : to get Last element
        System.out.println("Last element : " + d.peekLast());
  
  
        // poll() method : to get head
        System.out.println("\nHead element poll : " + d.poll());
  
        // pollFirst() method : to get First element
        System.out.println("First element poll : " + d.pollFirst());
  
        // pollLast() method : to get Last element
        System.out.println("Last element poll : " + d.pollLast() + "\n");
  
        for (Integer element : d)
        {
            System.out.println("Element : " + element);
        }
  
        // pop() method :
        System.out.println("Pop element : " + d.pop());
  
        // push() method :
        d.push(11010101);
        d.push(121212121);
        d.push(131313131);
  
        // remove() method : to get head
        System.out.println("\nHead element remove : " + d.remove());
  
        // removeFirst() method : to get First element
        System.out.println("First element remove : " + d.removeFirst());
  
        // removeLast() method : to get Last element
        System.out.println("Last element remove : " + d.removeLast() + "\n");
  
        for (Integer element : d)
        {
            System.out.println("Element : " + element);
        }
  
    }
}

chevron_right


Ouptut :

Element : 3333
Element : 1111
Element : 2
Element : 4
Element : 100
Element : 101
Element : 5000
Element : 50001

Head element : 3333
First element : 3333
Last element : 50001

Head element poll : 3333
First element poll : 1111
Last element poll : 50001

Element : 2
Element : 4
Element : 100
Element : 101
Element : 5000
Pop element : 2

Head element remove : 131313131
First element remove : 121212121
Last element remove : 5000

Element : 11010101
Element : 4
Element : 100
Element : 101

This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Shivam_k



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.