Collections

Arraydeque Java Code With Examples

Table of Contents

Program

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

public class ArrayDequeExample {
	public static void main(String[] args) {
		Deque<Integer> ven1 = new ArrayDeque<Integer>();
		
		//add​(E e)
		ven1.add(28);
		ven1.add(25);
		ven1.add(26);
		System.out.println("mark list of student1:" + ven1);
		
		// addAll​(Collection<? extends E> c)
		Deque<Integer> ven2 = new ArrayDeque<Integer>();
		ven2.add(22);
		ven2.add(19);
		ven2.add(20);
		System.out.println(" \nmark list of student2:" + ven2);
		ven1.addAll(ven2);
		System.out.println("\nThe total list of student mark : " + ven1);
		
		//peek()
		int ret = ven1.peek();
		System.out.println("\npeeked Element is: " + ret);
		System.out.println("\nArrayDeque: " + ven1);
		
		//poll()
		int reg = ven1.poll();
		System.out.println("\npolled Element is: " + reg);
		System.out.println("\nArrayDeque: " + ven1);

 		//isEmpty()
		System.out.println("\nIs ArrayDeque Empty: "+ven1.isEmpty());
	}
}

Output

mark list of student1:[28, 25, 26]
 
mark list of student2:[22, 19, 20]

The total list of student mark : [28, 25, 26, 22, 19, 20]

peeked Element is: 28

ArrayDeque: [28, 25, 26, 22, 19, 20]

polled Element is: 28

ArrayDeque: [25, 26, 22, 19, 20]

Is ArrayDeque Empty: false  

Description

public boolean add​(E e)

Inserts the specified element at the end of this deque.
This method is equivalent to addLast(E).

Parameters:

e – the element to add

Returns:

true (as specified by Collection.add(E))

Throws:

NullPointerException – if the specified element is null

public boolean addAll​(Collection<? extends E> c)

Adds all of the elements in the specified collection at the end of this deque, as if by calling addLast(E) on each one, in the order that they are returned by the collection’s iterator.

Parameters:

c – the elements to be inserted into this deque

Returns:

true if this deque changed as a result of the call

Throws:

NullPointerException – if the specified collection or any of its elements are null

public E peek()

Retrieves, but does not remove, the head of the queue represented by this deque, or returns null if this deque is empty.
This method is equivalent to Deque.peekFirst().

Specified by:

peek in interface Deque<E>

Specified by:

peek in interface Queue<E>

Returns:

the head of the queue represented by this deque, or null if this deque is empty

public E poll()

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.
This method is equivalent to Deque.pollFirst().

Specified by:

poll in interface Deque<E>

Specified by:

poll in interface Queue<E>

Returns:

the head of the queue represented by this deque, or null if this deque is empty

public boolean isEmpty()

Returns true if this deque contains no elements.

Specified by:

isEmpty in interface Collection<E>

Overrides:

isEmpty in class AbstractCollection<E>

Returns:

true if this deque contains no elements