Java.util.ArrayDeque Class in Java | Set 1
More Methods of util.ArrayDeque Class :
- 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
- 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
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- pop() : java.util.ArrayDeque.pop() : pops out an element for stack repesentyed by deque
Syntax :public E pop() Parameters : --------- Return : element at front
- 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 : ----------
- remove() : java.util.ArrayDeque.remove(): returns head element and also removes it
Syntax :public E remove() Parameters : ------ Return : head of the deque
- removeFirst() : java.util.ArrayDeque.remove(): returns first element and also removes it
Syntax :public E removeFirst() Parameters : ------ Return : first element of the deque
- removeLast() : java.util.ArrayDeque.remove(): returns last element and also removes it
Syntax :public E removeLast() Parameters : ------ Return : last element of the deque
- 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
- 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 :
// 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); } } } |
Output :
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.