Tuesday, 6 January 2015

Java : Collection Framework : TreeSet (HeadSet Inclusive)


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

TreeSetExample.java
import java.util.NavigableSet;
import java.util.TreeSet;

/*
 * Example of headSet(E fromElement,boolean inclusive) 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 less
         * than (or equal to, if inclusive is true) 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.
         */

        NavigableSet<Integer> navigableSet = treeSet.headSet(40, false);

        System.out.println("inclusive is false , navigableSet : "
                + navigableSet + "\n");

        navigableSet = treeSet.headSet(40, true);

        System.out.println("inclusive is true , navigableSet : " + navigableSet
                + "\n");

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

inclusive is false , navigableSet : [10, 20, 30]

inclusive is true , navigableSet : [10, 20, 30, 40]
To Download TreeSetDemoHeadSetInclusive Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/TreeSetDemoHeadSetInclusive.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