Home  >  Core Java

Example of ArrayDeque in Java

By Arvind Rai, December 24, 2012
1. ArrayDeque implements Deque interface and ArrayDeque are available from jdk1.6.
2. Deque is that queue which allows insert and remove of elements from both sides.
3. ArrayDeque is not thread safe. ArrayDeque allows unlimited insertion of elements.
4. ArrayDeque can be used as queue and stack both. When it is used as stack, it is faster than stack and when it is used as queue, it is faster than linkedlist.
5. Iterator object of ArrayDeque allows removal and modification of ArrayDeque elements.
package com.concretepage;

import java.util.ArrayDeque;
import java.util.Iterator;

public class ArrayDequeTest {
	public static void main(String... args){
		
		ArrayDeque<String> aq= new ArrayDeque<String>();
		aq.add("A");
		aq.add("B");
		aq.add("C");
		
		//offerFirst-adds elements at the front of the ArrayDeque 
		aq.offerFirst("D");
		
		//offerLast inserts the element at the last of ArrayDeque 
		aq.offerLast("E");
		
		Iterator<String> itr= aq.iterator();
		while(itr.hasNext()){
			System.out.println(itr.next());
		}
		
	}
}
Output
D
A
B
C
E
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us