Thursday, February 25, 2016

Java Arraydeque Class

| Thursday, February 25, 2016
  • ArrayDeque is ane of the collection framework fellow member which implements Deque, Cloneable together with Serializable interfaces.
  • ArrayDeque provides Resizable-array implementation thus it has no capacity restrictions together with it volition grow every bit per requirement.
  • In the absence of external synchronization, they are non thread safe. So they produce non permit multiple threads to access it concurrently.
  • Also ArrayDequenot permit to shop zip elements.
  • ArrayDeque Class is faster than LinkedList when used every bit a queue together with faster than Stack when used every bit a stack.
  • If ArrayDeque is modified afterward the iterator creation together with if it is non modified past times iterators ain method together with then it volition throw ConcurrentModificationException. So inwards concurrent modification, Iterator volition fail.
ArrayDeque is ane of the collection framework fellow member which implements Deque Java ArrayDeque Class

Important methods of ArrayDeque Class
  • boolean add(E e) : It volition insert given chemical share inwards ArrayDeque.
  • void addFirst(E e) : It volition add together given chemical share at the kickoff of ArrayDeque.
  • void addLast(E e) : It volition add together given chemical share at the destination of ArrayDeque.
  • void clear() : It volition take away all elements from ArrayDeque to clear it.
  • ArrayDeque<E> clone() : It volition render re-create of ArrayDeque.
  • boolean contains(Object o) : It volition render truthful if specified chemical share available inwards ArrayDeque.
  • Iterator<E> descendingIterator() : It volition render iterator inwards contrary sequential social club over the elements of ArrayDeque.
  • E element() : It volition croak head(first) chemical share from this ArrayDeque.
  • E getFirst() : It volition croak head(first) chemical share from this ArrayDeque.
  • E getLast() : It volition croak concluding chemical share from this ArrayDeque.
  • boolean isEmpty() : It volition render truthful if ArrayDeque is empty.
  • boolean offer(E e) : It volition insert specified chemical share at the destination of this ArrayDeque.
  • boolean offerFirst(E e) : It volition insert specified chemical share at the kickoff of this ArrayDeque.
  • boolean offerLast(E e) : It volition insert specified chemical share at the destination of this ArrayDeque.
  • E peek() : It volition recall together with render caput chemical share from ArrayDeque. Return zip if ArrayDeque is empty.
  • E peekFirst() : It volition recall together with render head(first) chemical share from ArrayDeque. Return zip if ArrayDeque is empty.
  • E peekLast() : It volition recall together with render concluding chemical share from ArrayDeque. Return zip if ArrayDeque is empty.
  • E poll() : It volition take away caput chemical share from ArrayDeque. Return zip if ArrayDeque is empty.
  • E pollFirst() : It volition take away head(first) chemical share from ArrayDeque. Return zip if ArrayDeque is empty.
  • E pollLast() : It volition take away concluding chemical share from ArrayDeque. Return zip if ArrayDeque is empty.
  • E pop() : It volition popular an chemical share from stack represented past times this ArrayDeque.
  • void push(E e) : It volition pushes an chemical share on stack represented past times this ArrayDeque.
  • E remove() : It volition take away caput chemical share of the ArrayDeque.
  • boolean remove(Object o) : It volition take away unmarried illustration of specified object from ArrayDeque.
  • E removeFirst() : It volition take away firstly chemical share from ArrayDeque.
  • boolean removeFirstOccurrence(Object o) : It volition take away firstly occurrence of the chemical share from ArrayDeque.
  • E removeLast() : It volition take away concluding chemical share from ArrayDeque.
  • boolean removeLastOccurrence(Object o) : It volition take away concluding occurrence of the chemical share from ArrayDeque.
  • int size() :It volition render size of ArrayDeque.
  • Object[] toArray() : It volition render elements array from ArrayDeque.
  • <T> T[] toArray(T[] a) : It volition render elements array from ArrayDeque. The runtime type of the returned array is that of the specified array.
Below given illustration volition exhibit you lot demo of higher upwardly ArrayDeque  method's usage.

ArrayDeque Example
package JAVAExamples;  import java.util.ArrayDeque; import java.util.Deque;  populace shape ArrayDequeExample {   populace static void main(String[] args) {   // Create ArrayDeque.   ArrayDeque<String> d = novel ArrayDeque<String>();   // Add elements inwards Deque.   d.add("one");   d.add("two");   d.add("three");   d.add("four");   d.add("five");   d.add("four");   d.add("six");   // Print Deque elements.   System.out.println("Deque elements are : " + d);    // Add chemical share at firstly of Deque.   d.addFirst("First Added");   System.out.println("Deque elements afterward addFirst are : " + d);    // Add chemical share at concluding of Deque.   d.addLast("Last Added");   System.out.println("Deque elements afterward addLast are : " + d);    // Get firstly chemical share from Deque.   System.out.println("First chemical share inwards deque is : " + d.getFirst());    // Get concluding chemical share from Deque.   System.out.println("Last chemical share inwards deque is : " + d.getLast());    // Get firstly chemical share from Deque using peek.   System.out.println("First chemical share inwards deque using peek is : " + d.peek());    // Get concluding chemical share from Deque using peekLast.   System.out.println("Last chemical share inwards deque using peekLast is : " + d.peekLast());    // Remove firstly chemical share from deque.   d.removeFirst();   System.out.println("Deque elements afterward removeFirst are  : " + d);    // Using pop.   d.pop();   System.out.println("Deque elements afterward popular are  : " + d);    // Using push.   d.push("First");   System.out.println("Deque elements afterward force are  : " + d);    // Remove LastOccurrence of chemical share from deque using removeLastOccurrence.   d.removeLastOccurrence("four");   System.out.println("Deque elements afterward removeLastOccurrence are  : "+ d);    // Get size of deque.   System.out.println("Size of Deque is  : " + d.size());  } }

Output :
Deque elements are : [one, two, three, four, five, four, six] Deque elements afterward addFirst are : [First Added, one, two, three, four, five, four, six] Deque elements afterward addLast are : [First Added, one, two, three, four, five, four, six, Last Added] First chemical share inwards deque is : First Added Last chemical share inwards deque is : Last Added First chemical share inwards deque using peek is : First Added Last chemical share inwards deque using peekLast is : Last Added Deque elements afterward removeFirst are  : [one, two, three, four, five, four, six, Last Added] Deque elements afterward popular are  : [two, three, four, five, four, six, Last Added] Deque elements afterward force are  : [First, two, three, four, five, four, six, Last Added] Deque elements afterward removeLastOccurrence are  : [First, two, three, four, five, six, Last Added] Size of Deque is  : 7

Related Posts