To get subset from TreeSet

To get subset from TreeSet

Previous Home Next

 

This Example shows how to get the subset from the TreeSet .

We can get the subset from the TreeSet by giving Specific range of values usin Subset () method of the TreeSet class.
 Subset () method of TreeSet class is used here to get the SubSet  of  the TreeSet. This method returns the value  ranging from the (inclusive to the exclusive)

The method subset is used as subSet (int from int to)
This will return the value within the range.

 

package Example;

import java.util.Iterator;
import java.util.TreeSet;
import java.util.SortedSet;

public class TreeSetSubSet {

/**
* @param args
*/
public static void main(String[] args) {
TreeSet Tst =new TreeSet();
Tst.add(new Integer("10"));
Tst.add(new Integer("17"));
Tst.add(new Integer("15"));
Tst.add(new Integer("19"));
Tst.add(new Integer("12"));
Tst.add(new Integer("13"));

SortedSet sortedSet = Tst.subSet(new Integer("10"),new Integer("20"));

System.out.println("SortedSet Contains : " + sortedSet);

// TODO Auto-generated method stub

}

}

SortedSet Contains : [10, 12, 13, 15, 17, 19]

Previous Home Next