TreeSet subSet Example

TreeSet class subSet example. This example shows you how to use subSet method.


TreeSet class subSet example. public NavigableSet subSet(Object fromElement, boolean fromInclusive, Object toElement, boolean toInclusive) Returns a view of the portion of this set whose elements range from fromElement to toElement. If fromElement and toElement are equal, the returned set is empty unless fromExclusive and toExclusive are both true.

Here is the code

/**
 * @ # SubSet.java
 * A class repersenting use to SubSet method 
 * of TreeSet class in java.util package
 * version 23 May 2008
 * author Rose India 
 */
import java.util.*;

public class SubSet {

    public static void main(String args[]) {
        // Create a new TreeSet object:
        TreeSet treeset = new TreeSet();

        // Create new element object and Add the elementreeset to the set:
        treeset.add(new Integer(2));
        treeset.add(new Integer(5));
        treeset.add(new Integer(7));

        System.out.println("treeset = " + treeset);


        NavigableSet nset = treeset.subSet(2, true, 7true);
        System.out.println("Subset of the treeset : " + nset);

        nset = treeset.subSet(2, true, 7false);
        System.out.println("Subset of the treeset : " + nset);

    }
}

Output
treeset = [2, 5, 7]
Subset of the treeset : [2, 5, 7]
Subset of the treeset : [2, 5]

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US