• September 3, 2014
  • Core Java
  • Comments Off on ArrayDeque in java

ArrayDeque in java

ArrayDeque:

ArrayDeque extends AbstractCollection and implements Deque. ArrayDeque are not thread safe. It not allows the null elements.

 Note: ArrayDeque are faster than Stack and LinkedList.

 ArrayDeque example:

ArrayDequeTest.java

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
 
/**
 * This class is used to show the ArrayDeque functionality.
 * @author javawithease
 */
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: Abstract classes in collection framework in java with example.
Previous Topic: PriorityQueue in java with example.

Related Topics:

Collection framework in java.
Collection interfaces in java.
Collection interface in java with example.
Set interface in java with example.
SortedSet interface in java with example.
List interface in java with example.
Map interface in java with example.
Daemon thread in java in java with example.
SortedMap interface in java with example.
Queue interface in java with example.
Deque interface in java with example.
Enumeration interface in java with example.
Collection classes in java with example.
HashSet in java with example.
LinkedHashSet in java with example.
TreeSet in java with example.
ArrayList in java with example.
LinkedList in java with example.
HashMap in java with example.
LinkedHashMap in java with example.
TreeMap in java with example.
PriorityQueue in java with example.
ArrayDeque in java with example.
Abstract classes in collection framework in java with example.
Sorting in java with example.
Comparable interface in java with example.
Comparator interface in java with example.
Properties class in java with example.
Hashtable in java with example.
ListIterator interface in java with example.

 


Comments are closed.