Monday, 29 December 2014

Java : Collection Framework : TreeSet (Iterator remove)


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

TreeSetExample.java
import java.util.Iterator;
import java.util.TreeSet;

/*
 * Example of Iterator remove() method.
 */
public class TreeSetExample
{
    public static void main(String[] args)
    {

        TreeSet<Integer> treeSet = new TreeSet<Integer>();

        treeSet.add(10);
        treeSet.add(50);
        treeSet.add(40);
        treeSet.add(20);
        treeSet.add(30);

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

        /*
         * Returns an iterator over the elements in this set in ascending order.
         */

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

        while (iterator.hasNext())
        {
            iterator.next();
            /*
             * Removes from the treeSet the last element that was returned by
             * next()
             */
            iterator.remove();
        }

        System.out.println("treeSet : " + treeSet + "\n");
    }
}
Output
treeSet : [10, 20, 30, 40, 50]

treeSet : []
To Download TreeSetDemoIteratorRemove Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeSetDemoIteratorRemove.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