Wednesday 12 November 2014

Java : Collection Framework : Deque (Add element in the front)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=w6aNKzJflaY&list=UUhwKlOVR041tngjerWxVccw

DequeExample.java
import java.util.Deque;
import java.util.LinkedList;

/*
 *  Example of addFirst(e) and offerFirst(e) methods.
 */
public class DequeExample
{

    public static void main( String[] args )
    {
        Deque<Integer> deque = new LinkedList<Integer>();

        /*
         * Inserts the specified element into this queue if it is possible to do
         * so immediately without violating capacity restrictions, returning
         * true upon success and throwing an IllegalStateException if no space
         * is currently available.
         */
        deque.add(100);
        deque.add(200);
        deque.add(300);

        System.out.println("deque : " + deque + "\n");

        /*
         * Inserts the specified element at the front of this deque if it is
         * possible to do so immediately without violating capacity
         * restrictions. When using a capacity-restricted deque, it is generally
         * preferable to use method offerFirst(E).
         */
        deque.addFirst(50);

        System.out.println("deque : " + deque + "\n");

        /*
         * Inserts the specified element at the front of this deque unless it
         * would violate capacity restrictions. When using a capacity-restricted
         * deque, this method is generally preferable to the addFirst(E) method,
         * which can fail to insert an element only by throwing an exception.
         * Parameters:
         */
        boolean isAdded = deque.offerFirst(10);
        System.out.println("isAdded : " + isAdded);
        System.out.println("deque : " + deque);

    }
}

Output
deque : [100, 200, 300]

deque : [50, 100, 200, 300]

isAdded : true
deque : [10, 50, 100, 200, 300]


To Download DequeDemoAdd Project Click the below link

https://sites.google.com/site/javaee4321/java-collections/DequeDemoAdd.zip?attredirects=0&d=1

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment