TreeSet subSet Example
TreeSet class subSet example. This example shows you how to use subSet method.
TreeSet class subSet example.public SortedSet subSet(Object fromElement,Object toElement) Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. (If fromElement and toElement are equal, the returned set is empty.)
Here is the code
/**
* @ # SubSet1.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 SubSet1 {
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);
SortedSet ss = treeset.subSet(2, 7);
System.out.println("Display the headSet : " + ss);
}
} |
Output
treeset = [2, 5, 7]
Display the headSet : [2, 5] |
|