Wednesday 12 November 2014

Java : Collection Framework : LinkedList (Remove elements using Iterator)


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


LinkedListExample.java

import java.util.Iterator;
import java.util.LinkedList;

/*
 * Example of remove() method of Iterator
 */
public class LinkedListExample
{

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

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

        Iterator<Integer> iterator = linkedList.iterator();

        /*
         * Using Iterator move the cursor in forward direction and remove
         * element one by one.
         */

        while (iterator.hasNext())
        {

            iterator.next();
            /*
             * Removes from the list the last element that was returned by
             * next() method
             */
            iterator.remove();

        }

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

    }

}

Output

linkedList : [100, 200, 300, 400, 500]
linkedList : []

To Download LinkedListDemoIteratorRemove Project Click the below link

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