Friday 7 November 2014

Java : Collection Framework : LinkedList (pollFirst & pollLast methods)


Click here to watch in Youtube : https://www.youtube.com/watch?v=43gKVo8mKLI

LinkedListExample.java
import java.util.LinkedList;

/*
 * Example of pollFirst() and pollLast() methods
 */
public class LinkedListExample
{

    public static void main( String[] args )
    {
        LinkedList<Integer> linkedList = new LinkedList<Integer>();
        linkedList.add(200);
        linkedList.add(300);
        linkedList.add(10000);
        linkedList.add(5000);
        
        System.out.println("linkedList : " + linkedList + "\n");
        
        /*
         * Retrieves and removes the first element of this list, or returns null
         * if this list is empty.
         */
        Integer value = linkedList.pollFirst();
        System.out.println("pollFirst : value " + value);
        System.out.println("linkedList : " + linkedList + "\n");

        /*
         * Retrieves and removes the last element of this list, or returns null
         * if this list is empty.
         */
        value = linkedList.pollLast();
        System.out.println("pollLast : value " + value);
        System.out.println( "linkedList : " + linkedList + "\n");

    }
}

Output
linkedList : [200, 300, 10000, 5000]

pollFirst : value 200
linkedList : [300, 10000, 5000]

pollLast : value 5000
linkedList : [300, 10000]

To Download LinkedListDemoPollFirstLast Project Click the below link

https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoPollFirstLast.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