CodesJava

Easy learning with example program codes

Deque interface in java


Deque interface:

Deque interface represents a double-ended-queue. A deque represents a linear collection of elements that support insertion, retrieval and removal of elements at both ends.

Deques can be used both as FIFO (first-in, first-out) and LIFO (last-in, first-out). In a deque all new elements can be inserted, retrieved and removed at both ends.

Commonly used methods of Deque interface:

1. addFirst(Object obj): Inserts the specified element at the front of this deque.

Syntax: public void addFirst(Object obj)

2. addLast(Object obj): Inserts the specified element at the end of this deque.

Syntax: public void addLast(Object obj)

3. offerFirst(Object obj): Inserts the specified element at the front of this deque. Returns true if the element was added to this deque, else false.

Syntax: public boolean offerFirst(Object obj)

4. offerLast(Object obj): Inserts the specified element at the end of this deque. Returns true if the element was added to this deque, else false.

Syntax: public boolean offerLast(Object obj)

5. removeFirst(): Returns and removes the first element of this deque.  It throws an exception if this deque is empty.

Syntax: public Object removeFirst()

6. removeLast(): Returns and removes the last element of this deque.  It throws an exception if this deque is empty.

Syntax: public Object removeLast()

7. pollFirst(): Returns and removes the first element of this deque. Returns null if this deque is empty.

Syntax: public Object pollFirst()

8. pollLast(): Returns and removes the last element of this deque.  Returns null if this deque is empty.

Syntax: public Object pollLast()

9. getFirst(): Returns but does not removes the first element of this deque.  It throws an exception if this deque is empty.

Syntax: public Object getFirst()

10. getLast(): Returns but does not removes the last element of this deque.  It throws an exception if this deque is empty.

Syntax: public Object getLast()

11. peekFirst(): Returns but does not removes the first element of this deque. Returns null if this deque is empty.

Syntax: public Object peakFirst()

12. peekLast(): Returns but does not removes the last element of this deque.  Returns null if this deque is empty.

Syntax: public Object peekLast()

13. removeFirstOccurrence(): Removes the first occurrence of the specified element from this deque. Returns true if specified element is removed from this deque otherwise returns false.

Syntax: public boolean removeFirstOccurrence()

14. removeLastOccurrence(): Removes the last occurrence of the specified element from this deque. Returns true if specified element is removed from this deque otherwise returns false.

Syntax: public boolean removeLastOccurrence()

 A simple example of ArrayDeque class to explain few methods of Deque interface.

 ArrayDequeTest.java

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
 
/**
 * This class is used to show the ArrayDeque functionality.
 * @author codesjava
 */
public class ArrayDequeTest {
	public static void main(String args[]){
		//Create ArrayDeque  object.
		Deque arrayDeque  = new ArrayDeque();
 
		//Add objects to the ArrayDeque.
		arrayDeque.add("Gourav");
		arrayDeque.add("Neeraj");
		arrayDeque.add("Deepak");
		arrayDeque.add("Mohan");
		arrayDeque.add("Parmender");
 
		//Print the ArrayDeque object.
		System.out.println("ArrayDeque elements:");
		System.out.println(arrayDeque);
 
		//Print the ArrayDeque elements using iterator.
		Iterator iterator1=arrayDeque.iterator();
		System.out.println("ArrayDeque elements " +
				"using iterator:");
		while(iterator1.hasNext()){  
		   System.out.println(iterator1.next());  
		}
 
		//Print the first element of the ArrayDeque.
		System.out.println("First element: " 
				+ arrayDeque.getFirst());
 
		//Print the last element of the ArrayDeque.
		System.out.println("Last element: " 
				+ arrayDeque.getLast());
 
		//Remove the first element of the ArrayDeque.
		arrayDeque.pollFirst();
 
		//Remove the last element of the ArrayDeque.
		arrayDeque.pollLast();
 
		//Print the ArrayDeque object.
		System.out.println("ArrayDeque elements " +
				"after manipulation:");
		System.out.println(arrayDeque);
 
		//Print the ArrayDeque elements using iterator.
		Iterator iterator2=arrayDeque.iterator();
		System.out.println("ArrayDeque elements after " +
				"manipulation using iterator:");
		while(iterator2.hasNext()){  
		   System.out.println(iterator2.next());  
		}
	}
}

Output:

ArrayDeque elements:
[Gourav, Neeraj, Deepak, Mohan, Parmender]
ArrayDeque elements using iterator:
Gourav
Neeraj
Deepak
Mohan
Parmender
First element: Gourav
Last element: Parmender
ArrayDeque elements after manipulation:
[Neeraj, Deepak, Mohan]
ArrayDeque elements after manipulation using iterator:
Neeraj
Deepak
Mohan

Download this example.
 
Next Topic: Enumeration interface in java with example.
Previous Topic: Queue interface in java with example.

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki