Friday 7 November 2014

Java : Collection Framework : LinkedList (removeFirstOccurrence & removeLastOccurrence methods)


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

LinkedListExample.java
import java.util.LinkedList;

/*
 * Example of removeFirstOccurrence(Object o) and removeLastOccurrence(Object o) methods
 */
public class LinkedListExample
{

    public static void main( String[] args )
    {
        LinkedList<Integer> linkedList = new LinkedList<Integer>();
        linkedList.add(200);
        linkedList.add(300);
        linkedList.add(200);
        linkedList.add(5000);
        linkedList.add(200);
        linkedList.add(800);

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

        /*
         * Removes the first occurrence of the specified element in this list
         * (when traversing the list from head to tail). If the list does not
         * contain the element, it is unchanged.
         */
        boolean isRemoved = linkedList.removeFirstOccurrence(200);
        System.out.println("isRemoved : " + isRemoved);
        System.out.println("linkedList : " + linkedList + "\n");

        /*
         * Removes the last occurrence of the specified element in this list
         * (when traversing the list from head to tail). If the list does not
         * contain the element, it is unchanged.
         */
        isRemoved = linkedList.removeLastOccurrence(200);
        System.out.println("isRemoved : " + isRemoved);
        System.out.println("linkedList : " + linkedList + "\n");

    }
}

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

isRemoved : true
linkedList : [300, 200, 5000, 200, 800]

isRemoved : true
linkedList : [300, 200, 5000, 800]

To Download LinkedListDemoRemoveFirstLastOcc Project Click the below link

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