TreeSet headSet Example
TreeSet class headSet example. This example shows you how to use headSet method.
TreeSet class headSet example.public SortedSet headSet(Object) 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.
Here is the code
/**
* @ # HeadSet.java
* A class repersenting use to headSet method
* of TreeSet class in java.util package
* version 23 May 2008
* author Rose India
*/
import java.util.*;
public class HeadSet {
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.headSet(5);
System.out.println("Display the headSet : " + ss);
}
} |
Output
treeset = [2, 5, 7]
Display the headSet : [2] |
|