Monday, 5 January 2015

Java : Collection Framework : TreeSet (HeadSet)


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

TreeSetExample.java
import java.util.SortedSet;
import java.util.TreeSet;

/*
 * Example of headSet(E toElement) method.
 */
public class TreeSetExample
{
    public static void main( String[] args )
    {

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

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

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

        /*
         * Returns a view of the portion of this set whose elements are strictly
         * less than toElement.
         * 
         * The returned set is backed by this set, so changes in the returned
         * set are reflected in this set, and vice-versa. The returned set
         * supports all optional set operations that this set supports.
         * 
         * Equivalent to headSet(toElement, false).
         */

        SortedSet<Integer> sortedSet = treeSet.headSet(40);
        System.out.println("sortedSet : " + sortedSet + "\n");

        System.out.println("--------------------------------------------\n");

        sortedSet.remove(10);

        System.out.println("After removing element '10' in SortedSet \n");
        System.out.println("sortedSet : " + sortedSet);
        System.out.println("treeSet : " + treeSet + "\n");

    }
}
Output
treeSet : [10, 20, 30, 40, 50]

sortedSet : [10, 20, 30]

--------------------------------------------

After removing element '10' in SortedSet 

sortedSet : [20, 30]
treeSet : [20, 30, 40, 50]
To Download TreeSetDemoHeadSet Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeSetDemoHeadSet.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

    Google+ Followers

    Google+ Badge